Refactored Android sample (1-java)
This commit is contained in:
parent
c3e9d0dd7e
commit
72a55a4139
@ -3,5 +3,5 @@
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="gen"/>
|
||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||
<classpathentry kind="output" path="bin1"/>
|
||||
<classpathentry kind="output" path="cls"/>
|
||||
</classpath>
|
||||
|
@ -8,45 +8,47 @@ import android.view.MenuItem;
|
||||
import android.view.Window;
|
||||
|
||||
public class Sample1Java extends Activity {
|
||||
private static final String TAG = "Sample1Java::Activity";
|
||||
private static final String TAG = "Sample::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;
|
||||
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;
|
||||
private MenuItem mItemPreviewRGBA;
|
||||
private MenuItem mItemPreviewGray;
|
||||
private MenuItem mItemPreviewCanny;
|
||||
private MenuItem mItemPreviewSobel;
|
||||
private MenuItem mItemPreviewBlur;
|
||||
|
||||
public static int viewMode = VIEW_MODE_RGBA;
|
||||
|
||||
public Sample1Java(){
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
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");
|
||||
Log.i(TAG, "onCreate");
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
setContentView(new Sample1View(this));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
Log.i(TAG, "onCreateOptionsMenu");
|
||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||
mItemPreviewGray = menu.add("Preview GRAY");
|
||||
Log.i(TAG, "onCreateOptionsMenu");
|
||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||
mItemPreviewGray = menu.add("Preview GRAY");
|
||||
mItemPreviewCanny = menu.add("Canny");
|
||||
mItemPreviewSobel = menu.add("Sobel");
|
||||
mItemPreviewBlur = menu.add("Blur");
|
||||
mItemPreviewBlur = menu.add("Blur");
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
Log.i(TAG, "Menu Item selected " + item);
|
||||
if (item == mItemPreviewRGBA)
|
||||
|
@ -15,40 +15,32 @@ class Sample1View extends SampleViewBase {
|
||||
public Sample1View(Context context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
super.surfaceChanged(_holder, format, width, height);
|
||||
super.surfaceChanged(_holder, format, width, height);
|
||||
|
||||
synchronized (this) {
|
||||
// initialize all required Mats before usage to minimize number of auxiliary jni calls
|
||||
if(mYuv != null) mYuv.dispose();
|
||||
mYuv = new Mat(getFrameHeight()+getFrameHeight()/2, getFrameWidth(), CvType.CV_8UC1);
|
||||
|
||||
if(mRgba != null) mRgba.dispose();
|
||||
mRgba = new Mat(getFrameHeight(), getFrameWidth(), CvType.CV_8UC4);
|
||||
|
||||
if(mGraySubmat != null) mGraySubmat.dispose();
|
||||
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
||||
synchronized (this) {
|
||||
// initialize Mats before usage
|
||||
mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
|
||||
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
||||
|
||||
if(mIntermediateMat != null) mIntermediateMat.dispose();
|
||||
mIntermediateMat = new Mat(getFrameHeight(), getFrameWidth(), CvType.CV_8UC1);
|
||||
}
|
||||
mRgba = new Mat();
|
||||
mIntermediateMat = new Mat();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Bitmap processFrame(byte[] data)
|
||||
{
|
||||
mYuv.put(0, 0, data);
|
||||
|
||||
switch(Sample1Java.viewMode)
|
||||
{
|
||||
protected Bitmap processFrame(byte[] data) {
|
||||
mYuv.put(0, 0, data);
|
||||
|
||||
switch (Sample1Java.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);
|
||||
core.putText(mRgba, "OpenCV + Android", new Point(10,100), 3/*CV_FONT_HERSHEY_COMPLEX*/, 2, new Scalar(255, 0, 0, 255), 3);
|
||||
core.putText(mRgba, "OpenCV + Android", new Point(10, 100), 3/* CV_FONT_HERSHEY_COMPLEX */, 2, new Scalar(255, 0, 0, 255), 3);
|
||||
break;
|
||||
case Sample1Java.VIEW_MODE_CANNY:
|
||||
imgproc.Canny(mGraySubmat, mIntermediateMat, 80, 100);
|
||||
@ -64,32 +56,34 @@ class Sample1View extends SampleViewBase {
|
||||
imgproc.blur(mRgba, mRgba, new Size(15, 15));
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Bitmap bmp = Bitmap.createBitmap(getFrameWidth(), getFrameHeight(), Bitmap.Config.ARGB_8888);
|
||||
android.MatToBitmap(mRgba, bmp);
|
||||
|
||||
return bmp;
|
||||
|
||||
if (android.MatToBitmap(mRgba, bmp))
|
||||
return bmp;
|
||||
|
||||
bmp.recycle();
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
super.run();
|
||||
|
||||
// Explicitly deallocate Mats
|
||||
if(mYuv != null) {
|
||||
mYuv.dispose();
|
||||
super.run();
|
||||
|
||||
synchronized (this) {
|
||||
// Explicitly deallocate Mats
|
||||
if (mYuv != null)
|
||||
mYuv.dispose();
|
||||
if (mRgba != null)
|
||||
mRgba.dispose();
|
||||
if (mGraySubmat != null)
|
||||
mGraySubmat.dispose();
|
||||
if (mIntermediateMat != null)
|
||||
mIntermediateMat.dispose();
|
||||
|
||||
mYuv = null;
|
||||
}
|
||||
if(mRgba != null) {
|
||||
mRgba.dispose();
|
||||
mRgba = null;
|
||||
}
|
||||
if(mGraySubmat != null) {
|
||||
mGraySubmat.dispose();
|
||||
mGraySubmat = null;
|
||||
}
|
||||
if(mIntermediateMat != null) {
|
||||
mIntermediateMat.dispose();
|
||||
mIntermediateMat = null;
|
||||
}
|
||||
}
|
||||
|
@ -12,110 +12,108 @@ 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 Camera mCamera;
|
||||
private SurfaceHolder mHolder;
|
||||
private int mFrameWidth;
|
||||
private int mFrameHeight;
|
||||
private byte[] mFrame;
|
||||
private boolean mThreadRun;
|
||||
private static final String TAG = "Sample::SurfaceView";
|
||||
|
||||
public SampleViewBase(Context context) {
|
||||
super(context);
|
||||
private Camera mCamera;
|
||||
private SurfaceHolder mHolder;
|
||||
private int mFrameWidth;
|
||||
private int mFrameHeight;
|
||||
private byte[] mFrame;
|
||||
private boolean mThreadRun;
|
||||
|
||||
public SampleViewBase(Context context) {
|
||||
super(context);
|
||||
mHolder = getHolder();
|
||||
mHolder.addCallback(this);
|
||||
Log.i(TAG, "Instantiated new " + this.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
public int getFrameWidth() {
|
||||
return mFrameWidth;
|
||||
}
|
||||
public int getFrameWidth() {
|
||||
return mFrameWidth;
|
||||
}
|
||||
|
||||
public int getFrameHeight() {
|
||||
return mFrameHeight;
|
||||
}
|
||||
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();
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
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();
|
||||
mFrameWidth = width;
|
||||
mFrameHeight = height;
|
||||
|
||||
params.setPreviewSize(getFrameWidth(), getFrameHeight());
|
||||
mCamera.setParameters(params);
|
||||
mCamera.startPreview();
|
||||
}
|
||||
}
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
Log.i(TAG, "surfaceCreated");
|
||||
mCamera = Camera.open();
|
||||
mCamera.setPreviewCallback(
|
||||
new PreviewCallback() {
|
||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||
synchronized(SampleViewBase.this) {
|
||||
mFrame = data;
|
||||
SampleViewBase.this.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
(new Thread(this)).start();
|
||||
}
|
||||
params.setPreviewSize(getFrameWidth(), getFrameHeight());
|
||||
mCamera.setParameters(params);
|
||||
mCamera.startPreview();
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
Log.i(TAG, "surfaceDestroyed");
|
||||
mThreadRun = false;
|
||||
if(mCamera != null) {
|
||||
synchronized(this) {
|
||||
mCamera.stopPreview();
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Bitmap processFrame(byte[] data);
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
Log.i(TAG, "surfaceCreated");
|
||||
mCamera = Camera.open();
|
||||
mCamera.setPreviewCallback(new PreviewCallback() {
|
||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||
synchronized (SampleViewBase.this) {
|
||||
mFrame = data;
|
||||
SampleViewBase.this.notify();
|
||||
}
|
||||
}
|
||||
});
|
||||
(new Thread(this)).start();
|
||||
}
|
||||
|
||||
public void run() {
|
||||
mThreadRun = true;
|
||||
Log.i(TAG, "Starting processing thread");
|
||||
while(mThreadRun) {
|
||||
Bitmap bmp = null;
|
||||
|
||||
synchronized(this) {
|
||||
try {
|
||||
this.wait();
|
||||
bmp = processFrame(mFrame);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (bmp != null) {
|
||||
Canvas canvas = mHolder.lockCanvas();
|
||||
if (canvas != null){
|
||||
canvas.drawBitmap(bmp, (canvas.getWidth()-getFrameWidth())/2, (canvas.getHeight()-getFrameHeight())/2, null);
|
||||
mHolder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
bmp.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
Log.i(TAG, "surfaceDestroyed");
|
||||
mThreadRun = false;
|
||||
if (mCamera != null) {
|
||||
synchronized (this) {
|
||||
mCamera.stopPreview();
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Bitmap processFrame(byte[] data);
|
||||
|
||||
public void run() {
|
||||
mThreadRun = true;
|
||||
Log.i(TAG, "Starting processing thread");
|
||||
while (mThreadRun) {
|
||||
Bitmap bmp = null;
|
||||
|
||||
synchronized (this) {
|
||||
try {
|
||||
this.wait();
|
||||
bmp = processFrame(mFrame);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
if (bmp != null) {
|
||||
Canvas canvas = mHolder.lockCanvas();
|
||||
if (canvas != null) {
|
||||
canvas.drawBitmap(bmp, (canvas.getWidth() - getFrameWidth()) / 2, (canvas.getHeight() - getFrameHeight()) / 2, null);
|
||||
mHolder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
bmp.recycle();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user