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="src"/>
|
||||||
<classpathentry kind="src" path="gen"/>
|
<classpathentry kind="src" path="gen"/>
|
||||||
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
|
||||||
<classpathentry kind="output" path="bin1"/>
|
<classpathentry kind="output" path="cls"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
@ -8,7 +8,7 @@ import android.view.MenuItem;
|
|||||||
import android.view.Window;
|
import android.view.Window;
|
||||||
|
|
||||||
public class Sample1Java extends Activity {
|
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_RGBA = 0;
|
||||||
public static final int VIEW_MODE_GRAY = 1;
|
public static final int VIEW_MODE_GRAY = 1;
|
||||||
@ -37,6 +37,7 @@ public class Sample1Java extends Activity {
|
|||||||
setContentView(new Sample1View(this));
|
setContentView(new Sample1View(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
Log.i(TAG, "onCreateOptionsMenu");
|
Log.i(TAG, "onCreateOptionsMenu");
|
||||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||||
@ -47,6 +48,7 @@ public class Sample1Java extends Activity {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
public boolean onOptionsItemSelected(MenuItem item) {
|
||||||
Log.i(TAG, "Menu Item selected " + item);
|
Log.i(TAG, "Menu Item selected " + item);
|
||||||
if (item == mItemPreviewRGBA)
|
if (item == mItemPreviewRGBA)
|
||||||
|
@ -21,28 +21,20 @@ class Sample1View extends SampleViewBase {
|
|||||||
super.surfaceChanged(_holder, format, width, height);
|
super.surfaceChanged(_holder, format, width, height);
|
||||||
|
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
// initialize all required Mats before usage to minimize number of auxiliary jni calls
|
// initialize Mats before usage
|
||||||
if(mYuv != null) mYuv.dispose();
|
|
||||||
mYuv = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
|
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());
|
mGraySubmat = mYuv.submat(0, getFrameHeight(), 0, getFrameWidth());
|
||||||
|
|
||||||
if(mIntermediateMat != null) mIntermediateMat.dispose();
|
mRgba = new Mat();
|
||||||
mIntermediateMat = new Mat(getFrameHeight(), getFrameWidth(), CvType.CV_8UC1);
|
mIntermediateMat = new Mat();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Bitmap processFrame(byte[] data)
|
protected Bitmap processFrame(byte[] data) {
|
||||||
{
|
|
||||||
mYuv.put(0, 0, data);
|
mYuv.put(0, 0, data);
|
||||||
|
|
||||||
switch(Sample1Java.viewMode)
|
switch (Sample1Java.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);
|
||||||
break;
|
break;
|
||||||
@ -66,30 +58,32 @@ class Sample1View extends SampleViewBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Bitmap bmp = Bitmap.createBitmap(getFrameWidth(), getFrameHeight(), Bitmap.Config.ARGB_8888);
|
Bitmap bmp = Bitmap.createBitmap(getFrameWidth(), getFrameHeight(), Bitmap.Config.ARGB_8888);
|
||||||
android.MatToBitmap(mRgba, bmp);
|
|
||||||
|
|
||||||
|
if (android.MatToBitmap(mRgba, bmp))
|
||||||
return bmp;
|
return bmp;
|
||||||
|
|
||||||
|
bmp.recycle();
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
super.run();
|
super.run();
|
||||||
|
|
||||||
|
synchronized (this) {
|
||||||
// Explicitly deallocate Mats
|
// Explicitly deallocate Mats
|
||||||
if(mYuv != null) {
|
if (mYuv != null)
|
||||||
mYuv.dispose();
|
mYuv.dispose();
|
||||||
mYuv = null;
|
if (mRgba != null)
|
||||||
}
|
|
||||||
if(mRgba != null) {
|
|
||||||
mRgba.dispose();
|
mRgba.dispose();
|
||||||
mRgba = null;
|
if (mGraySubmat != null)
|
||||||
}
|
|
||||||
if(mGraySubmat != null) {
|
|
||||||
mGraySubmat.dispose();
|
mGraySubmat.dispose();
|
||||||
mGraySubmat = null;
|
if (mIntermediateMat != null)
|
||||||
}
|
|
||||||
if(mIntermediateMat != null) {
|
|
||||||
mIntermediateMat.dispose();
|
mIntermediateMat.dispose();
|
||||||
|
|
||||||
|
mYuv = null;
|
||||||
|
mRgba = null;
|
||||||
|
mGraySubmat = null;
|
||||||
mIntermediateMat = null;
|
mIntermediateMat = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ 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::SurfaceView";
|
||||||
|
|
||||||
private Camera mCamera;
|
private Camera mCamera;
|
||||||
private SurfaceHolder mHolder;
|
private SurfaceHolder mHolder;
|
||||||
@ -65,16 +65,14 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
|
|||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
Log.i(TAG, "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;
|
||||||
SampleViewBase.this.notify();
|
SampleViewBase.this.notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
|
||||||
(new Thread(this)).start();
|
(new Thread(this)).start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user