mirror of
https://github.com/opencv/opencv.git
synced 2025-01-18 06:03: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 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. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
setContentView(new Sample1View(this));
|
||||
viewMode = VIEW_MODE_RGBA;
|
||||
}
|
||||
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
Log.i(TAG, "onCreateOptionsMenu");
|
||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||
mItemPreviewGray = menu.add("Preview GRAY");
|
||||
mItemPreviewCanny = menu.add("Canny");
|
||||
|
@ -1,21 +1,12 @@
|
||||
package org.opencv.samples.s1;
|
||||
|
||||
import org.opencv.*;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
import org.opencv.CvType;
|
||||
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 {
|
||||
class Sample1View extends SampleViewBase {
|
||||
Mat mYuv;
|
||||
Mat mRgba;
|
||||
Mat mGraySubmat;
|
||||
@ -28,35 +19,29 @@ class Sample1View extends SampleViewBase implements SurfaceHolder.Callback {
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
super.surfaceChanged(_holder, format, width, height);
|
||||
Log.e("SAMP1", "surfaceChanged begin");
|
||||
|
||||
synchronized (this) {
|
||||
Log.e("SAMP1", "surfaceChanged sync");
|
||||
// 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, CvType.CV_8UC1);
|
||||
mYuv = new Mat(getFrameHeight()+getFrameHeight()/2, getFrameWidth(), CvType.CV_8UC1);
|
||||
|
||||
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();
|
||||
mGraySubmat = mYuv.submat(0, mFrameHeight, 0, mFrameWidth);
|
||||
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
||||
|
||||
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
|
||||
protected Bitmap processFrame(byte[] data)
|
||||
{
|
||||
//Log.e("SAMP1", "processFrame begin");
|
||||
|
||||
mYuv.put(0, 0, data);
|
||||
|
||||
Sample1Java a = (Sample1Java)getContext();
|
||||
|
||||
switch(a.viewMode)
|
||||
switch(Sample1Java.viewMode)
|
||||
{
|
||||
case Sample1Java.VIEW_MODE_GRAY:
|
||||
imgproc.cvtColor(mGraySubmat, mRgba, imgproc.CV_GRAY2RGBA, 4);
|
||||
@ -80,20 +65,17 @@ class Sample1View extends SampleViewBase implements SurfaceHolder.Callback {
|
||||
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);
|
||||
|
||||
//Log.e("SAMP1", "processFrame end");
|
||||
return bmp;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Log.e("SAMP1", "run");
|
||||
super.run();
|
||||
Log.e("SAMP1", "run2");
|
||||
|
||||
// Explicitly release Mats
|
||||
// Explicitly deallocate Mats
|
||||
if(mYuv != null) {
|
||||
mYuv.dispose();
|
||||
mYuv = null;
|
||||
|
@ -12,12 +12,12 @@ import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
||||
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 SurfaceHolder mHolder;
|
||||
protected int mFrameWidth;
|
||||
protected int mFrameHeight;
|
||||
private int mFrameWidth;
|
||||
private int mFrameHeight;
|
||||
private byte[] mFrame;
|
||||
private boolean mThreadRun;
|
||||
|
||||
@ -25,9 +25,19 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
super(context);
|
||||
mHolder = getHolder();
|
||||
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) {
|
||||
Log.i(TAG, "surfaceCreated");
|
||||
if ( mCamera != null) {
|
||||
Camera.Parameters params = mCamera.getParameters();
|
||||
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.startPreview();
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
Log.i("SAMP1", "surfaceCreated");
|
||||
Log.i(TAG, "surfaceCreated");
|
||||
mCamera = Camera.open();
|
||||
mCamera.setPreviewCallback(
|
||||
new PreviewCallback() {
|
||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||
synchronized(SampleViewBase.this) {
|
||||
mFrame = data;
|
||||
//Log.i("SAMP1", "before 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) {
|
||||
Log.i("SAMP1", "surfaceDestroyed");
|
||||
Log.i(TAG, "surfaceDestroyed");
|
||||
mThreadRun = false;
|
||||
if(mCamera != null) {
|
||||
synchronized(this) {
|
||||
@ -86,17 +95,14 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
|
||||
public void run() {
|
||||
mThreadRun = true;
|
||||
Log.i(TAG, "Starting thread");
|
||||
Bitmap bmp = null;
|
||||
Log.i(TAG, "Starting processing thread");
|
||||
while(mThreadRun) {
|
||||
//Log.i("SAMP1", "before synchronized");
|
||||
Bitmap bmp = null;
|
||||
|
||||
synchronized(this) {
|
||||
//Log.i("SAMP1", "in synchronized");
|
||||
try {
|
||||
this.wait();
|
||||
//Log.i("SAMP1", "before processFrame");
|
||||
bmp = processFrame(mFrame);
|
||||
//Log.i("SAMP1", "after processFrame");
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -105,9 +111,10 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
||||
if (bmp != null) {
|
||||
Canvas canvas = mHolder.lockCanvas();
|
||||
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);
|
||||
}
|
||||
bmp.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user