Autofocus issues on Android ICS fixed. Continuous-video focus mode is set.

Performance problems fixed. Shared camera buffer is used.
This commit is contained in:
Alexander Smorkalov
2012-04-09 07:36:54 +00:00
parent 7cc7a3f37d
commit be20370a3d
11 changed files with 414 additions and 184 deletions

View File

@@ -10,13 +10,10 @@ import android.view.Window;
public class Sample0Base extends Activity {
private static final String TAG = "Sample::Activity";
public static final int VIEW_MODE_RGBA = 0;
public static final int VIEW_MODE_GRAY = 1;
private MenuItem mItemPreviewRGBA;
private MenuItem mItemPreviewGray;
private Sample0View mView;
public static int viewMode = VIEW_MODE_RGBA;
public Sample0Base() {
Log.i(TAG, "Instantiated new " + this.getClass());
@@ -28,7 +25,8 @@ public class Sample0Base extends Activity {
Log.i(TAG, "onCreate");
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Sample0View(this));
mView = new Sample0View(this);
setContentView(mView);
}
@Override
@@ -43,9 +41,9 @@ public class Sample0Base extends Activity {
public boolean onOptionsItemSelected(MenuItem item) {
Log.i(TAG, "Menu Item selected " + item);
if (item == mItemPreviewRGBA)
viewMode = VIEW_MODE_RGBA;
mView.setViewMode(Sample0View.VIEW_MODE_RGBA);
else if (item == mItemPreviewGray)
viewMode = VIEW_MODE_GRAY;
mView.setViewMode(Sample0View.VIEW_MODE_GRAY);
return true;
}
}

View File

@@ -2,34 +2,52 @@ package org.opencv.samples.tutorial0;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
class Sample0View extends SampleViewBase {
private static final String TAG = "Sample0View";
int mSize;
int[] mRGBA;
private Bitmap mBitmap;
private int mViewMode;
public static final int VIEW_MODE_RGBA = 0;
public static final int VIEW_MODE_GRAY = 1;
public Sample0View(Context context) {
super(context);
mSize = 0;
mViewMode = VIEW_MODE_RGBA;
}
@Override
protected Bitmap processFrame(byte[] data) {
int frameSize = getFrameWidth() * getFrameHeight();
int[] rgba = new int[frameSize];
int[] rgba = mRGBA;
int view_mode = Sample0Base.viewMode;
if (view_mode == Sample0Base.VIEW_MODE_GRAY) {
final int view_mode = mViewMode;
if (view_mode == VIEW_MODE_GRAY) {
for (int i = 0; i < frameSize; i++) {
int y = (0xff & ((int) data[i]));
rgba[i] = 0xff000000 + (y << 16) + (y << 8) + y;
}
} else if (view_mode == Sample0Base.VIEW_MODE_RGBA) {
} else if (view_mode == VIEW_MODE_RGBA) {
for (int i = 0; i < getFrameHeight(); i++)
for (int j = 0; j < getFrameWidth(); j++) {
int y = (0xff & ((int) data[i * getFrameWidth() + j]));
int u = (0xff & ((int) data[frameSize + (i >> 1) * getFrameWidth() + (j & ~1) + 0]));
int v = (0xff & ((int) data[frameSize + (i >> 1) * getFrameWidth() + (j & ~1) + 1]));
int index = i * getFrameWidth() + j;
int supply_index = frameSize + (i >> 1) * getFrameWidth() + (j & ~1);
int y = (0xff & ((int) data[index]));
int u = (0xff & ((int) data[supply_index + 0]));
int v = (0xff & ((int) data[supply_index + 1]));
y = y < 16 ? 16 : y;
int r = Math.round(1.164f * (y - 16) + 1.596f * (v - 128));
int g = Math.round(1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128));
int b = Math.round(1.164f * (y - 16) + 2.018f * (u - 128));
float y_conv = 1.164f * (y - 16);
int r = Math.round(y_conv + 1.596f * (v - 128));
int g = Math.round(y_conv - 0.813f * (v - 128) - 0.391f * (u - 128));
int b = Math.round(y_conv + 2.018f * (u - 128));
r = r < 0 ? 0 : (r > 255 ? 255 : r);
g = g < 0 ? 0 : (g > 255 ? 255 : g);
@@ -38,9 +56,26 @@ class Sample0View extends SampleViewBase {
rgba[i * getFrameWidth() + j] = 0xff000000 + (b << 16) + (g << 8) + r;
}
}
Bitmap bmp = Bitmap.createBitmap(getFrameWidth(), getFrameHeight(), Bitmap.Config.ARGB_8888);
bmp.setPixels(rgba, 0/* offset */, getFrameWidth() /* stride */, 0, 0, getFrameWidth(), getFrameHeight());
return bmp;
mBitmap.setPixels(rgba, 0/* offset */, getFrameWidth() /* stride */, 0, 0, getFrameWidth(), getFrameHeight());
return mBitmap;
}
@Override
protected void onPreviewStared(int previewWidth, int previewHeight) {
/* Create a bitmap that will be used through to calculate the image to */
mBitmap = Bitmap.createBitmap(previewWidth, previewHeight, Bitmap.Config.ARGB_8888);
mRGBA = new int[previewWidth * previewHeight];
}
@Override
protected void onPreviewStopped() {
mBitmap.recycle();
mBitmap = null;
mRGBA = null;
}
public void setViewMode(int viewMode) {
mViewMode = viewMode;
}
}

View File

@@ -6,6 +6,7 @@ import java.util.List;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.Camera.PreviewCallback;
@@ -23,6 +24,8 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
private int mFrameHeight;
private byte[] mFrame;
private boolean mThreadRun;
private byte[] mBuffer;
public SampleViewBase(Context context) {
super(context);
@@ -43,9 +46,10 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
mCamera.setPreviewTexture( new SurfaceTexture(10) );
else
mCamera.setPreviewDisplay(null);
}
mCamera.setPreviewDisplay(null);
}
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
Log.i(TAG, "surfaceCreated");
if (mCamera != null) {
@@ -56,7 +60,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
// selecting optimal camera preview size
{
double minDiff = Double.MAX_VALUE;
int minDiff = Integer.MAX_VALUE;
for (Camera.Size size : sizes) {
if (Math.abs(size.height - height) < minDiff) {
mFrameWidth = size.width;
@@ -67,12 +71,28 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
}
params.setPreviewSize(getFrameWidth(), getFrameHeight());
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
mCamera.setParameters(params);
try {
setPreview();
/* Now allocate the buffer */
params = mCamera.getParameters();
int size = params.getPreviewSize().width * params.getPreviewSize().height;
size = size * ImageFormat.getBitsPerPixel(params.getPreviewFormat()) / 8;
mBuffer = new byte[size];
/* The buffer where the current frame will be coppied */
mFrame = new byte [size];
mCamera.addCallbackBuffer(mBuffer);
try {
setPreview();
} catch (IOException e) {
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e);
}
/* Notify that the preview is about to be started and deliver preview size */
onPreviewStared(params.getPreviewSize().width, params.getPreviewSize().height);
/* Now we can start a preview */
mCamera.startPreview();
}
}
@@ -80,14 +100,17 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
public void surfaceCreated(SurfaceHolder holder) {
Log.i(TAG, "surfaceCreated");
mCamera = Camera.open();
mCamera.setPreviewCallback(new PreviewCallback() {
mCamera.setPreviewCallbackWithBuffer(new PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
synchronized (SampleViewBase.this) {
mFrame = data;
SampleViewBase.this.notify();
System.arraycopy(data, 0, mFrame, 0, data.length);
SampleViewBase.this.notify();
}
camera.addCallbackBuffer(mBuffer);
}
});
(new Thread(this)).start();
}
@@ -102,10 +125,27 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
mCamera = null;
}
}
onPreviewStopped();
}
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */
protected abstract Bitmap processFrame(byte[] data);
/**
* This method is called when the preview process is beeing started. It is called before the first frame delivered and processFrame is called
* It is called with the width and height parameters of the preview process. It can be used to prepare the data needed during the frame processing.
* @param previewWidth - the width of the preview frames that will be delivered via processFrame
* @param previewHeight - the height of the preview frames that will be delivered via processFrame
*/
protected abstract void onPreviewStared(int previewWidtd, int previewHeight);
/**
* This method is called when preview is stopped. When this method is called the preview stopped and all the processing of frames already completed.
* If the Bitmap object returned via processFrame is cached - it is a good time to recycle it.
* Any other resourcses used during the preview can be released.
*/
protected abstract void onPreviewStopped();
public void run() {
mThreadRun = true;
Log.i(TAG, "Starting processing thread");
@@ -127,7 +167,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde
canvas.drawBitmap(bmp, (canvas.getWidth() - getFrameWidth()) / 2, (canvas.getHeight() - getFrameHeight()) / 2, null);
mHolder.unlockCanvasAndPost(canvas);
}
bmp.recycle();
}
}
}