mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 14:13:15 +08:00
Fixed android sample
This commit is contained in:
parent
c8a603d853
commit
a9508d2f67
@ -22,18 +22,23 @@ public class Sample1Java extends Activity {
|
|||||||
private MenuItem mItemPreviewSobel;
|
private MenuItem mItemPreviewSobel;
|
||||||
private MenuItem mItemPreviewBlur;
|
private MenuItem mItemPreviewBlur;
|
||||||
|
|
||||||
public int viewMode;
|
public static int viewMode = VIEW_MODE_RGBA;
|
||||||
|
|
||||||
|
public Sample1Java(){
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
|
Log.i(TAG, "onCreate");
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
setContentView(new Sample1View(this));
|
setContentView(new Sample1View(this));
|
||||||
viewMode = VIEW_MODE_RGBA;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
|
Log.i(TAG, "onCreateOptionsMenu");
|
||||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||||
mItemPreviewGray = menu.add("Preview GRAY");
|
mItemPreviewGray = menu.add("Preview GRAY");
|
||||||
mItemPreviewCanny = menu.add("Canny");
|
mItemPreviewCanny = menu.add("Canny");
|
||||||
|
@ -1,21 +1,12 @@
|
|||||||
package org.opencv.samples.s1;
|
package org.opencv.samples.s1;
|
||||||
|
|
||||||
|
import org.opencv.*;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.util.Log;
|
|
||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
|
|
||||||
import org.opencv.CvType;
|
class Sample1View extends SampleViewBase {
|
||||||
import org.opencv.Mat;
|
|
||||||
import org.opencv.Point;
|
|
||||||
import org.opencv.Scalar;
|
|
||||||
import org.opencv.Size;
|
|
||||||
import org.opencv.core;
|
|
||||||
import org.opencv.imgproc;
|
|
||||||
import org.opencv.android;
|
|
||||||
|
|
||||||
|
|
||||||
class Sample1View extends SampleViewBase implements SurfaceHolder.Callback {
|
|
||||||
Mat mYuv;
|
Mat mYuv;
|
||||||
Mat mRgba;
|
Mat mRgba;
|
||||||
Mat mGraySubmat;
|
Mat mGraySubmat;
|
||||||
@ -28,35 +19,29 @@ class Sample1View extends SampleViewBase implements SurfaceHolder.Callback {
|
|||||||
@Override
|
@Override
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
super.surfaceChanged(_holder, format, width, height);
|
super.surfaceChanged(_holder, format, width, height);
|
||||||
Log.e("SAMP1", "surfaceChanged begin");
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
Log.e("SAMP1", "surfaceChanged sync");
|
|
||||||
// initialize all required Mats before usage to minimize number of auxiliary jni calls
|
// initialize all required Mats before usage to minimize number of auxiliary jni calls
|
||||||
if(mYuv != null) mYuv.dispose();
|
if(mYuv != null) mYuv.dispose();
|
||||||
mYuv = new Mat(mFrameHeight+mFrameHeight/2, mFrameWidth, CvType.CV_8UC1);
|
mYuv = new Mat(getFrameHeight()+getFrameHeight()/2, getFrameWidth(), CvType.CV_8UC1);
|
||||||
|
|
||||||
if(mRgba != null) mRgba.dispose();
|
if(mRgba != null) mRgba.dispose();
|
||||||
mRgba = new Mat(mFrameHeight, mFrameWidth, CvType.CV_8UC4);
|
mRgba = new Mat(getFrameHeight(), getFrameWidth(), CvType.CV_8UC4);
|
||||||
|
|
||||||
if(mGraySubmat != null) mGraySubmat.dispose();
|
if(mGraySubmat != null) mGraySubmat.dispose();
|
||||||
mGraySubmat = mYuv.submat(0, mFrameHeight, 0, mFrameWidth);
|
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
||||||
|
|
||||||
if(mIntermediateMat != null) mIntermediateMat.dispose();
|
if(mIntermediateMat != null) mIntermediateMat.dispose();
|
||||||
mIntermediateMat = new Mat(mFrameHeight, mFrameWidth, CvType.CV_8UC1);
|
mIntermediateMat = new Mat(getFrameHeight(), getFrameWidth(), CvType.CV_8UC1);
|
||||||
}
|
}
|
||||||
Log.e("SAMP1", "surfaceChanged end");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Bitmap processFrame(byte[] data)
|
protected Bitmap processFrame(byte[] data)
|
||||||
{
|
{
|
||||||
//Log.e("SAMP1", "processFrame begin");
|
|
||||||
|
|
||||||
mYuv.put(0, 0, data);
|
mYuv.put(0, 0, data);
|
||||||
|
|
||||||
Sample1Java a = (Sample1Java)getContext();
|
switch(Sample1Java.viewMode)
|
||||||
|
|
||||||
switch(a.viewMode)
|
|
||||||
{
|
{
|
||||||
case Sample1Java.VIEW_MODE_GRAY:
|
case Sample1Java.VIEW_MODE_GRAY:
|
||||||
imgproc.cvtColor(mGraySubmat, mRgba, imgproc.CV_GRAY2RGBA, 4);
|
imgproc.cvtColor(mGraySubmat, mRgba, imgproc.CV_GRAY2RGBA, 4);
|
||||||
@ -80,20 +65,17 @@ class Sample1View extends SampleViewBase implements SurfaceHolder.Callback {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bitmap bmp = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.ARGB_8888);
|
Bitmap bmp = Bitmap.createBitmap(getFrameWidth(), getFrameHeight(), Bitmap.Config.ARGB_8888);
|
||||||
android.MatToBitmap(mRgba, bmp);
|
android.MatToBitmap(mRgba, bmp);
|
||||||
|
|
||||||
//Log.e("SAMP1", "processFrame end");
|
|
||||||
return bmp;
|
return bmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Log.e("SAMP1", "run");
|
|
||||||
super.run();
|
super.run();
|
||||||
Log.e("SAMP1", "run2");
|
|
||||||
|
|
||||||
// Explicitly release Mats
|
// Explicitly deallocate Mats
|
||||||
if(mYuv != null) {
|
if(mYuv != null) {
|
||||||
mYuv.dispose();
|
mYuv.dispose();
|
||||||
mYuv = null;
|
mYuv = null;
|
||||||
|
@ -12,12 +12,12 @@ import android.view.SurfaceHolder;
|
|||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
public abstract class SampleViewBase extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
|
private static final String TAG = "SampleViewBase";
|
||||||
|
|
||||||
private static final String TAG = "Sample::ViewBase";
|
|
||||||
private Camera mCamera;
|
private Camera mCamera;
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
protected int mFrameWidth;
|
private int mFrameWidth;
|
||||||
protected int mFrameHeight;
|
private int mFrameHeight;
|
||||||
private byte[] mFrame;
|
private byte[] mFrame;
|
||||||
private boolean mThreadRun;
|
private boolean mThreadRun;
|
||||||
|
|
||||||
@ -25,9 +25,19 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
super(context);
|
super(context);
|
||||||
mHolder = getHolder();
|
mHolder = getHolder();
|
||||||
mHolder.addCallback(this);
|
mHolder.addCallback(this);
|
||||||
|
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFrameWidth() {
|
||||||
|
return mFrameWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getFrameHeight() {
|
||||||
|
return mFrameHeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
|
Log.i(TAG, "surfaceCreated");
|
||||||
if ( mCamera != null) {
|
if ( mCamera != null) {
|
||||||
Camera.Parameters params = mCamera.getParameters();
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
@ -45,23 +55,22 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
params.setPreviewSize(mFrameWidth, mFrameHeight);
|
|
||||||
|
params.setPreviewSize(getFrameWidth(), getFrameHeight());
|
||||||
mCamera.setParameters(params);
|
mCamera.setParameters(params);
|
||||||
mCamera.startPreview();
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i("SAMP1", "surfaceCreated");
|
Log.i(TAG, "surfaceCreated");
|
||||||
mCamera = Camera.open();
|
mCamera = Camera.open();
|
||||||
mCamera.setPreviewCallback(
|
mCamera.setPreviewCallback(
|
||||||
new PreviewCallback() {
|
new PreviewCallback() {
|
||||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||||
synchronized(SampleViewBase.this) {
|
synchronized(SampleViewBase.this) {
|
||||||
mFrame = data;
|
mFrame = data;
|
||||||
//Log.i("SAMP1", "before notify");
|
|
||||||
SampleViewBase.this.notify();
|
SampleViewBase.this.notify();
|
||||||
//Log.i("SAMP1", "after notify");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,7 +79,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
Log.i("SAMP1", "surfaceDestroyed");
|
Log.i(TAG, "surfaceDestroyed");
|
||||||
mThreadRun = false;
|
mThreadRun = false;
|
||||||
if(mCamera != null) {
|
if(mCamera != null) {
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
@ -86,17 +95,14 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
|
|
||||||
public void run() {
|
public void run() {
|
||||||
mThreadRun = true;
|
mThreadRun = true;
|
||||||
Log.i(TAG, "Starting thread");
|
Log.i(TAG, "Starting processing thread");
|
||||||
Bitmap bmp = null;
|
|
||||||
while(mThreadRun) {
|
while(mThreadRun) {
|
||||||
//Log.i("SAMP1", "before synchronized");
|
Bitmap bmp = null;
|
||||||
|
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
//Log.i("SAMP1", "in synchronized");
|
|
||||||
try {
|
try {
|
||||||
this.wait();
|
this.wait();
|
||||||
//Log.i("SAMP1", "before processFrame");
|
|
||||||
bmp = processFrame(mFrame);
|
bmp = processFrame(mFrame);
|
||||||
//Log.i("SAMP1", "after processFrame");
|
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
@ -105,9 +111,10 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
if (bmp != null) {
|
if (bmp != null) {
|
||||||
Canvas canvas = mHolder.lockCanvas();
|
Canvas canvas = mHolder.lockCanvas();
|
||||||
if (canvas != null){
|
if (canvas != null){
|
||||||
canvas.drawBitmap(bmp, (canvas.getWidth()-mFrameWidth)/2, (canvas.getHeight()-mFrameHeight)/2, null);
|
canvas.drawBitmap(bmp, (canvas.getWidth()-getFrameWidth())/2, (canvas.getHeight()-getFrameHeight())/2, null);
|
||||||
mHolder.unlockCanvasAndPost(canvas);
|
mHolder.unlockCanvasAndPost(canvas);
|
||||||
}
|
}
|
||||||
|
bmp.recycle();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user