mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 06:03:15 +08:00
Added second sample for Android Java API
This commit is contained in:
parent
96d29621b7
commit
6e3142f04b
@ -1,6 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
package="org.opencv.samples"
|
package="org.opencv.samples.s0"
|
||||||
android:versionCode="1"
|
android:versionCode="1"
|
||||||
android:versionName="1.0">
|
android:versionName="1.0">
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package org.opencv.samples;
|
package org.opencv.samples.s0;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
@ -1,4 +1,4 @@
|
|||||||
package org.opencv.samples;
|
package org.opencv.samples.s0;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
@ -69,10 +69,12 @@ class Sample0View extends SurfaceView implements SurfaceHolder.Callback, Runnabl
|
|||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
mThreadRun = false;
|
mThreadRun = false;
|
||||||
if(mCamera != null) {
|
if(mCamera != null) {
|
||||||
mCamera.stopPreview();
|
synchronized(Sample0View.this) {
|
||||||
mCamera.setPreviewCallback(null);
|
mCamera.stopPreview();
|
||||||
mCamera.release();
|
mCamera.setPreviewCallback(null);
|
||||||
mCamera = null;
|
mCamera.release();
|
||||||
|
mCamera = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
8
samples/android/1-java/.classpath
Normal file
8
samples/android/1-java/.classpath
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" path="src"/>
|
||||||
|
<classpathentry kind="src" path="gen"/>
|
||||||
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||||
|
<classpathentry kind="src" path="OpenCVJavaAPI_src"/>
|
||||||
|
<classpathentry kind="output" path="bin"/>
|
||||||
|
</classpath>
|
40
samples/android/1-java/.project
Normal file
40
samples/android/1-java/.project
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>Sample 1 Java</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
</natures>
|
||||||
|
<linkedResources>
|
||||||
|
<link>
|
||||||
|
<name>OpenCVJavaAPI_src</name>
|
||||||
|
<type>2</type>
|
||||||
|
<locationURI>_android_OpenCVJavaAPI_583dbd7b/src</locationURI>
|
||||||
|
</link>
|
||||||
|
</linkedResources>
|
||||||
|
</projectDescription>
|
@ -0,0 +1,5 @@
|
|||||||
|
#Wed Jun 29 04:36:40 MSD 2011
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.5
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.5
|
29
samples/android/1-java/AndroidManifest.xml
Normal file
29
samples/android/1-java/AndroidManifest.xml
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
package="org.opencv.samples.s1"
|
||||||
|
android:versionCode="1"
|
||||||
|
android:versionName="1.0">
|
||||||
|
|
||||||
|
<supports-screens android:resizeable="true"
|
||||||
|
android:smallScreens="true"
|
||||||
|
android:normalScreens="true"
|
||||||
|
android:largeScreens="true"
|
||||||
|
android:anyDensity="true" />
|
||||||
|
|
||||||
|
<application android:label="@string/app_name" android:icon="@drawable/icon">
|
||||||
|
<activity android:name="Sample1Java"
|
||||||
|
android:label="@string/app_name">
|
||||||
|
<intent-filter>
|
||||||
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
|
</intent-filter>
|
||||||
|
</activity>
|
||||||
|
</application>
|
||||||
|
|
||||||
|
<uses-sdk android:minSdkVersion="8" />
|
||||||
|
|
||||||
|
<uses-permission android:name="android.permission.CAMERA"/>
|
||||||
|
<uses-feature android:name="android.hardware.camera" />
|
||||||
|
<uses-feature android:name="android.hardware.camera.autofocus" />
|
||||||
|
|
||||||
|
</manifest>
|
12
samples/android/1-java/default.properties
Normal file
12
samples/android/1-java/default.properties
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# This file is automatically generated by Android Tools.
|
||||||
|
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
|
||||||
|
#
|
||||||
|
# This file must be checked in Version Control Systems.
|
||||||
|
#
|
||||||
|
# To customize properties used by the Ant build system use,
|
||||||
|
# "build.properties", and override values to adapt the script to your
|
||||||
|
# project structure.
|
||||||
|
|
||||||
|
# Project target.
|
||||||
|
target=android-8
|
||||||
|
android.library.reference.1=../../../android/build
|
BIN
samples/android/1-java/res/drawable/icon.png
Normal file
BIN
samples/android/1-java/res/drawable/icon.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 5.6 KiB |
4
samples/android/1-java/res/values/strings.xml
Normal file
4
samples/android/1-java/res/values/strings.xml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<resources>
|
||||||
|
<string name="app_name">Sample 1: Java API</string>
|
||||||
|
</resources>
|
@ -0,0 +1,59 @@
|
|||||||
|
package org.opencv.samples.s1;
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.Menu;
|
||||||
|
import android.view.MenuItem;
|
||||||
|
import android.view.Window;
|
||||||
|
|
||||||
|
public class Sample1Java extends Activity {
|
||||||
|
private static final String TAG = "Sample1Java::Activity";
|
||||||
|
|
||||||
|
public static final int VIEW_MODE_RGBA = 0;
|
||||||
|
public static final int VIEW_MODE_GRAY = 1;
|
||||||
|
public static final int VIEW_MODE_CANNY = 2;
|
||||||
|
public static final int VIEW_MODE_SOBEL = 3;
|
||||||
|
public static final int VIEW_MODE_BLUR = 4;
|
||||||
|
|
||||||
|
private MenuItem mItemPreviewRGBA;
|
||||||
|
private MenuItem mItemPreviewGray;
|
||||||
|
private MenuItem mItemPreviewCanny;
|
||||||
|
private MenuItem mItemPreviewSobel;
|
||||||
|
private MenuItem mItemPreviewBlur;
|
||||||
|
|
||||||
|
public int viewMode;
|
||||||
|
|
||||||
|
/** Called when the activity is first created. */
|
||||||
|
@Override
|
||||||
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
super.onCreate(savedInstanceState);
|
||||||
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
|
setContentView(new Sample1View(this));
|
||||||
|
viewMode = VIEW_MODE_RGBA;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||||
|
mItemPreviewGray = menu.add("Preview GRAY");
|
||||||
|
mItemPreviewCanny = menu.add("Canny");
|
||||||
|
mItemPreviewSobel = menu.add("Sobel");
|
||||||
|
mItemPreviewBlur = menu.add("Blur");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
|
Log.i(TAG, "Menu Item selected " + item);
|
||||||
|
if (item == mItemPreviewRGBA)
|
||||||
|
viewMode = VIEW_MODE_RGBA;
|
||||||
|
else if (item == mItemPreviewGray)
|
||||||
|
viewMode = VIEW_MODE_GRAY;
|
||||||
|
else if (item == mItemPreviewCanny)
|
||||||
|
viewMode = VIEW_MODE_CANNY;
|
||||||
|
else if (item == mItemPreviewSobel)
|
||||||
|
viewMode = VIEW_MODE_SOBEL;
|
||||||
|
else if (item == mItemPreviewBlur)
|
||||||
|
viewMode = VIEW_MODE_BLUR;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,169 @@
|
|||||||
|
package org.opencv.samples.s1;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
|
import android.graphics.Canvas;
|
||||||
|
import android.hardware.Camera;
|
||||||
|
import android.hardware.Camera.PreviewCallback;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.SurfaceHolder;
|
||||||
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
|
import org.opencv.Mat;
|
||||||
|
import org.opencv.Size;
|
||||||
|
import org.opencv.core;
|
||||||
|
import org.opencv.imgproc;
|
||||||
|
import org.opencv.utils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
class Sample1View extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
|
private static final String TAG = "Sample1Java::View";
|
||||||
|
|
||||||
|
private Camera mCamera;
|
||||||
|
private SurfaceHolder mHolder;
|
||||||
|
private int mFrameWidth;
|
||||||
|
private int mFrameHeight;
|
||||||
|
private byte[] mFrame;
|
||||||
|
private boolean mThreadRun;
|
||||||
|
|
||||||
|
private Mat mYuv;
|
||||||
|
private Mat mRgba;
|
||||||
|
private Mat mGraySubmat;
|
||||||
|
private Mat mIntermediateMat;
|
||||||
|
|
||||||
|
public Sample1View(Context context) {
|
||||||
|
super(context);
|
||||||
|
mHolder = getHolder();
|
||||||
|
mHolder.addCallback(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
|
if ( mCamera != null) {
|
||||||
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
|
mFrameWidth = width;
|
||||||
|
mFrameHeight = height;
|
||||||
|
|
||||||
|
//selecting optimal camera preview size
|
||||||
|
{
|
||||||
|
double minDiff = Double.MAX_VALUE;
|
||||||
|
for (Camera.Size size : sizes) {
|
||||||
|
if (Math.abs(size.height - height) < minDiff) {
|
||||||
|
mFrameWidth = size.width;
|
||||||
|
mFrameHeight = size.height;
|
||||||
|
minDiff = Math.abs(size.height - height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
params.setPreviewSize(mFrameWidth, mFrameHeight);
|
||||||
|
mCamera.setParameters(params);
|
||||||
|
mCamera.startPreview();
|
||||||
|
|
||||||
|
// initialize all required Mats before usage to minimize number of auxiliary jni calls
|
||||||
|
if(mYuv != null) mYuv.dispose();
|
||||||
|
mYuv = new Mat(mFrameHeight+mFrameHeight/2, mFrameWidth, Mat.CvType.CV_8UC1);
|
||||||
|
|
||||||
|
if(mRgba != null) mRgba.dispose();
|
||||||
|
mRgba = new Mat(mFrameHeight, mFrameWidth, Mat.CvType.CV_8UC4);
|
||||||
|
|
||||||
|
if(mGraySubmat != null) mGraySubmat.dispose();
|
||||||
|
mGraySubmat = mYuv.submat(0, mFrameHeight, 0, mFrameWidth);
|
||||||
|
|
||||||
|
if(mIntermediateMat != null) mIntermediateMat.dispose();
|
||||||
|
mIntermediateMat = new Mat(mFrameHeight, mFrameWidth, Mat.CvType.CV_8UC1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
|
mCamera = Camera.open();
|
||||||
|
mCamera.setPreviewCallback(
|
||||||
|
new PreviewCallback() {
|
||||||
|
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||||
|
synchronized(Sample1View.this) {
|
||||||
|
mFrame = data;
|
||||||
|
Sample1View.this.notify();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
(new Thread(this)).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
|
mThreadRun = false;
|
||||||
|
if(mCamera != null) {
|
||||||
|
synchronized(Sample1View.this) {
|
||||||
|
mCamera.stopPreview();
|
||||||
|
mCamera.setPreviewCallback(null);
|
||||||
|
mCamera.release();
|
||||||
|
mCamera = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Explicitly dispose Mats
|
||||||
|
if(mYuv != null) {
|
||||||
|
mYuv.dispose();
|
||||||
|
mYuv = null;
|
||||||
|
}
|
||||||
|
if(mRgba != null) {
|
||||||
|
mRgba.dispose();
|
||||||
|
mRgba = null;
|
||||||
|
}
|
||||||
|
if(mGraySubmat != null) {
|
||||||
|
mGraySubmat.dispose();
|
||||||
|
mGraySubmat = null;
|
||||||
|
}
|
||||||
|
if(mIntermediateMat != null) {
|
||||||
|
mIntermediateMat.dispose();
|
||||||
|
mIntermediateMat = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void run() {
|
||||||
|
mThreadRun = true;
|
||||||
|
Log.i(TAG, "Starting thread");
|
||||||
|
while(mThreadRun) {
|
||||||
|
synchronized(this) {
|
||||||
|
try {
|
||||||
|
this.wait();
|
||||||
|
mYuv.put(0, 0, mFrame);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Sample1Java a = (Sample1Java)getContext();
|
||||||
|
|
||||||
|
switch(a.viewMode)
|
||||||
|
{
|
||||||
|
case Sample1Java.VIEW_MODE_GRAY:
|
||||||
|
imgproc.cvtColor(mGraySubmat, mRgba, imgproc.CV_GRAY2RGBA, 4);
|
||||||
|
break;
|
||||||
|
case Sample1Java.VIEW_MODE_RGBA:
|
||||||
|
imgproc.cvtColor(mYuv, mRgba, imgproc.CV_YUV420i2RGB, 4);
|
||||||
|
break;
|
||||||
|
case Sample1Java.VIEW_MODE_CANNY:
|
||||||
|
imgproc.Canny(mGraySubmat, mIntermediateMat, 80, 100);
|
||||||
|
imgproc.cvtColor(mIntermediateMat, mRgba, imgproc.CV_GRAY2BGRA, 4);
|
||||||
|
break;
|
||||||
|
case Sample1Java.VIEW_MODE_SOBEL:
|
||||||
|
imgproc.Sobel(mGraySubmat, mIntermediateMat, core.CV_8U, 1, 1);
|
||||||
|
core.convertScaleAbs(mIntermediateMat, mIntermediateMat, 8);
|
||||||
|
imgproc.cvtColor(mIntermediateMat, mRgba, imgproc.CV_GRAY2BGRA, 4);
|
||||||
|
break;
|
||||||
|
case Sample1Java.VIEW_MODE_BLUR:
|
||||||
|
imgproc.cvtColor(mYuv, mRgba, imgproc.CV_YUV420i2RGB, 4);
|
||||||
|
imgproc.blur(mRgba, mRgba, new Size(15, 15));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
Bitmap bmp = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.ARGB_8888);
|
||||||
|
utils.MatToBitmap(mRgba, bmp);
|
||||||
|
|
||||||
|
Canvas canvas = mHolder.lockCanvas();
|
||||||
|
canvas.drawBitmap(bmp, (canvas.getWidth()-mFrameWidth)/2, (canvas.getHeight()-mFrameHeight)/2, null);
|
||||||
|
mHolder.unlockCanvasAndPost(canvas);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user