Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

android-examples-recording-plus #11920

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
11 changes: 5 additions & 6 deletions wrappers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ if (BUILD_PYTHON_BINDINGS OR BUILD_PYTHON_DOCS)
if (NOT INTERNET_CONNECTION)
message(WARNING "No internet connection. Cloning Python bindings may fail")
endif()
add_subdirectory(python)

add_subdirectory(python)
endif()

if (BUILD_NODEJS_BINDINGS)
add_subdirectory(nodejs)
endif()

if (BUILD_CV_EXAMPLES)
add_subdirectory(opencv)
Expand Down Expand Up @@ -62,7 +65,3 @@ endif()
if(BUILD_OPENNI2_BINDINGS)
add_subdirectory(openni2)
endif()

if(BUILD_PC_STITCHING)
add_subdirectory(pointcloud/pointcloud-stitching)
endif()
13 changes: 10 additions & 3 deletions wrappers/android/examples/capture/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 30
compileSdkVersion 28
defaultConfig {
applicationId "com.intel.realsense.capture"
minSdkVersion 19
targetSdkVersion 30
minSdkVersion 26
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
Expand All @@ -16,12 +16,19 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
buildToolsVersion '27.0.3'
ndkVersion '21.0.6113669'
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation project(path: ':openCVLibrary')
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,59 @@
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.intel.realsense.librealsense.Align;
import com.intel.realsense.librealsense.Colorizer;
import com.intel.realsense.librealsense.Config;
import com.intel.realsense.librealsense.DepthFrame;
import com.intel.realsense.librealsense.DeviceList;
import com.intel.realsense.librealsense.DeviceListener;
import com.intel.realsense.librealsense.Extension;
import com.intel.realsense.librealsense.Frame;
import com.intel.realsense.librealsense.FrameReleaser;
import com.intel.realsense.librealsense.FrameSet;
import com.intel.realsense.librealsense.GLRsSurfaceView;
import com.intel.realsense.librealsense.Pipeline;
import com.intel.realsense.librealsense.PipelineProfile;
import com.intel.realsense.librealsense.RsContext;
import com.intel.realsense.librealsense.StreamType;
import com.intel.realsense.librealsense.VideoFrame;

import org.opencv.android.OpenCVLoader;
import org.opencv.core.CvType;
import org.opencv.core.Mat;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends AppCompatActivity {

static {
if(!OpenCVLoader.initDebug())
{
Log.d("opencv","初始化失败");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log in Chinese??

}
}

private static final String TAG = "librs capture example";
private static final int PERMISSIONS_REQUEST_CAMERA = 0;
private static final int PERMISSIONS_REQUEST_WRITE = 1;

private boolean mPermissionsGranted = false;

Expand All @@ -47,12 +77,22 @@ protected void onCreate(Bundle savedInstanceState) {
mAppContext = getApplicationContext();
mBackGroundText = findViewById(R.id.connectCameraText);
mGLSurfaceView = findViewById(R.id.glSurfaceView);

Button button1 = (Button) findViewById(R.id.startRecordFab);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
toggleRecording();
}
});

mGLSurfaceView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);


// Android 9 also requires camera permissions
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.O &&
Expand All @@ -61,6 +101,11 @@ protected void onCreate(Bundle savedInstanceState) {
return;
}

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
return;
}

mPermissionsGranted = true;
}

Expand All @@ -73,9 +118,15 @@ protected void onDestroy() {
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAMERA);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_WRITE);
return;
}

if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
return;
}

mPermissionsGranted = true;
}

Expand Down Expand Up @@ -144,10 +195,9 @@ public void onDeviceDetach() {
@Override
public void run() {
try {
try(FrameSet frames = mPipeline.waitForFrames()) {
try(FrameSet processed = frames.applyFilter(mColorizer)) {
mGLSurfaceView.upload(processed);
}
try(FrameReleaser fr = new FrameReleaser()) {
FrameSet framesProcessed = mPipeline.waitForFrames().releaseWith(fr);
mGLSurfaceView.upload(framesProcessed);
}
mHandler.post(mStreaming);
}
Expand Down Expand Up @@ -196,4 +246,85 @@ private synchronized void stop() {
Log.d(TAG, "failed to stop streaming");
}
}

public void toggleRecording() {
try (FrameReleaser fr = new FrameReleaser()) {
try (FrameSet frames = mPipeline.waitForFrames().releaseWith(fr)) {
// Frame depth = frames.first(StreamType.DEPTH).releaseWith(fr);
// DepthFrame depthFrame = depth.as(Extension.DEPTH_FRAME);
// depthFrame.releaseWith(fr);
// saving raw depth data
// Mat mDepth = new Mat(depthFrame.getHeight(), depthFrame.getWidth(), CvType.CV_16UC1);
// int size = (int) (mDepth.total() * mDepth.elemSize());
// byte[] return_buff_depth = new byte[size];
// depthFrame.getData(return_buff_depth);
// short[] shorts = new short[size / 2];
// ByteBuffer.wrap(return_buff_depth).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
// mDepth.put(0, 0, shorts);
Align align = new Align(StreamType.COLOR);
String folderPath = "/storage/emulated/0/graduateDesign/";
String profile = "lhh";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateAndTime = sdf.format(new Date());

//Path path = Paths.get(folderPath, String.format("%spixelDepthData%s.npy", profile, currentDateAndTime));
//NpyFile.write(path, shorts);
// applying Colorizer
try (FrameSet processed = frames.applyFilter(align).releaseWith(fr).applyFilter(mColorizer).releaseWith(fr)) {
Frame depthColored = processed.first(StreamType.DEPTH).releaseWith(fr);
DepthFrame depthColorFrame = depthColored.as(Extension.DEPTH_FRAME);
depthColorFrame.releaseWith(fr);
Frame color = processed.first(StreamType.COLOR).releaseWith(fr);
VideoFrame colorFrame = color.as(Extension.VIDEO_FRAME);
colorFrame.releaseWith(fr);

// capturing color image
Mat mRGB = new Mat(colorFrame.getHeight(), colorFrame.getWidth(), CvType.CV_8UC3);
byte[] return_buff = new byte[colorFrame.getDataSize()];
colorFrame.getData(return_buff);
mRGB.put(0, 0, return_buff);

Bitmap bmp = Bitmap.createBitmap(mRGB.cols(), mRGB.rows(), Bitmap.Config.ARGB_8888);
org.opencv.android.Utils.matToBitmap(mRGB, bmp);

File imgFile = new File(folderPath, profile + "colorFrame-" + currentDateAndTime + ".jpg");
try {
FileOutputStream out2 = new FileOutputStream(imgFile);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out2);
out2.flush();
out2.close();
} catch (Exception e) {
e.printStackTrace();
}

// capturing colorized depth image
Mat mDepthColorized = new Mat(depthColorFrame.getHeight(), depthColorFrame.getWidth(), CvType.CV_8UC3);
byte[] return_buff_DepthColor = new byte[depthColorFrame.getDataSize()];
depthColorFrame.getData(return_buff_DepthColor);
mDepthColorized.put(0, 0, return_buff_DepthColor);

Bitmap bmpDepthColor = Bitmap.createBitmap(mDepthColorized.cols(), mDepthColorized.rows(), Bitmap.Config.ARGB_8888);
org.opencv.android.Utils.matToBitmap(mDepthColorized, bmpDepthColor);

File imgFileDepthColor = new File(folderPath, profile + "depthFrame-" + currentDateAndTime + ".jpg");
try {
FileOutputStream out3 = new FileOutputStream(imgFileDepthColor);
bmpDepthColor.compress(Bitmap.CompressFormat.JPEG, 100, out3);
out3.flush();
out3.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/black"
android:orientation="horizontal">

<com.intel.realsense.librealsense.GLRsSurfaceView
android:id="@+id/glSurfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/glSurfaceView"/>
android:layout_height="match_parent" />

<androidx.appcompat.widget.LinearLayoutCompat
android:id="@+id/buttonPanel"
Expand All @@ -29,5 +30,17 @@
android:textColor="#ffffff"/>
</androidx.appcompat.widget.LinearLayoutCompat>

<Button android:text="@string/message1"
android:id="@+id/startRecordFab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="40dp"
android:backgroundTint="#ffffff"
android:clickable="true"
android:visibility="visible" />



</RelativeLayout>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">RS Capture</string>
<string name="message1">拍照</string>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message in chinese???

</resources>
14 changes: 7 additions & 7 deletions wrappers/android/examples/java_example/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 30
compileSdkVersion 28

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compile sdk should be the latest not an old one

defaultConfig {
applicationId "com.example.realsense_java_example"
minSdkVersion 19
targetSdkVersion 30
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this? androidx is now the preferred way

}
buildTypes {
release {
Expand All @@ -20,11 +20,11 @@ android {

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'com.android.support:appcompat-v7:28.0.0'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here too androidX is the preferred way

implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

//The lines below add librealsense to the application's dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AndroidX is the preferred way

xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
Expand All @@ -16,4 +16,4 @@
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
6 changes: 3 additions & 3 deletions wrappers/android/examples/java_example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ buildscript {

repositories {
google()
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.2.1'
classpath 'com.android.tools.build:gradle:3.2.1'


// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -18,7 +18,7 @@ buildscript {
allprojects {
repositories {
google()
mavenCentral()
jcenter()
}
}

Expand Down
2 changes: 0 additions & 2 deletions wrappers/android/examples/java_example/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ org.gradle.jvmargs=-Xmx1536m
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.enableJetifier=true
android.useAndroidX=true