Android sample is refactored according to Google code style guidelines for Android developers
This commit is contained in:
parent
f27796e8cc
commit
334cb1dcd1
@ -0,0 +1,5 @@
|
|||||||
|
#Wed Jun 29 04:36:40 MSD 2011
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
|
||||||
|
org.eclipse.jdt.core.compiler.compliance=1.5
|
||||||
|
org.eclipse.jdt.core.compiler.source=1.5
|
@ -10,35 +10,35 @@ import android.view.Window;
|
|||||||
public class Sample0Base extends Activity {
|
public class Sample0Base extends Activity {
|
||||||
private static final String TAG = "Sample0Base::Activity";
|
private static final String TAG = "Sample0Base::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;
|
||||||
|
|
||||||
private MenuItem item_preview_rgba;
|
private MenuItem mItemPreviewRGBA;
|
||||||
private MenuItem item_preview_gray;
|
private MenuItem mItemPreviewGray;
|
||||||
|
|
||||||
public int view_mode;
|
public int viewMode;
|
||||||
|
|
||||||
/** 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) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||||
setContentView( new Sample0View(this) );
|
setContentView(new Sample0View(this));
|
||||||
view_mode = view_mode_rgba;
|
viewMode = VIEW_MODE_RGBA;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
public boolean onCreateOptionsMenu(Menu menu) {
|
||||||
item_preview_rgba = menu.add("Preview RGBA");
|
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||||
item_preview_gray = menu.add("Preview GRAY");
|
mItemPreviewGray = menu.add("Preview GRAY");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
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 == item_preview_rgba)
|
if (item == mItemPreviewRGBA)
|
||||||
view_mode = view_mode_rgba;
|
viewMode = VIEW_MODE_RGBA;
|
||||||
else if (item == item_preview_gray)
|
else if (item == mItemPreviewGray)
|
||||||
view_mode = view_mode_gray;
|
viewMode = VIEW_MODE_GRAY;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package org.opencv.samples;
|
package org.opencv.samples;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
@ -10,57 +9,55 @@ import android.util.Log;
|
|||||||
import android.view.SurfaceHolder;
|
import android.view.SurfaceHolder;
|
||||||
import android.view.SurfaceView;
|
import android.view.SurfaceView;
|
||||||
|
|
||||||
class Sample0View extends SurfaceView implements SurfaceHolder.Callback, Runnable{
|
import java.util.List;
|
||||||
|
|
||||||
|
class Sample0View extends SurfaceView implements SurfaceHolder.Callback, Runnable {
|
||||||
private static final String TAG = "Sample0Base::View";
|
private static final String TAG = "Sample0Base::View";
|
||||||
|
|
||||||
private Camera camera;
|
private Camera mCamera;
|
||||||
private SurfaceHolder holder;
|
private SurfaceHolder mHolder;
|
||||||
private int frame_width;
|
private int mFrameWidth;
|
||||||
private int frame_height;
|
private int mFrameHeight;
|
||||||
private byte[] frame;
|
private byte[] mFrame;
|
||||||
|
|
||||||
private boolean mThreadRun;
|
private boolean mThreadRun;
|
||||||
|
|
||||||
|
|
||||||
public Sample0View(Context context) {
|
public Sample0View(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
holder = getHolder();
|
mHolder = getHolder();
|
||||||
holder.addCallback(this);
|
mHolder.addCallback(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||||
if ( camera != null) {
|
if ( mCamera != null) {
|
||||||
Camera.Parameters params = camera.getParameters();
|
Camera.Parameters params = mCamera.getParameters();
|
||||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||||
frame_width = width;
|
mFrameWidth = width;
|
||||||
frame_height = height;
|
mFrameHeight = height;
|
||||||
|
|
||||||
//selecting optimal camera preview size
|
//selecting optimal camera preview size
|
||||||
{
|
{
|
||||||
double minDiff = Double.MAX_VALUE;
|
double minDiff = Double.MAX_VALUE;
|
||||||
for (Camera.Size size : sizes) {
|
for (Camera.Size size : sizes) {
|
||||||
if (Math.abs(size.height - height) < minDiff) {
|
if (Math.abs(size.height - height) < minDiff) {
|
||||||
frame_width = size.width;
|
mFrameWidth = size.width;
|
||||||
frame_height = size.height;
|
mFrameHeight = size.height;
|
||||||
minDiff = Math.abs(size.height - height);
|
minDiff = Math.abs(size.height - height);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
params.setPreviewSize(frame_width, frame_height);
|
params.setPreviewSize(mFrameWidth, mFrameHeight);
|
||||||
camera.setParameters(params);
|
mCamera.setParameters(params);
|
||||||
camera.startPreview();
|
mCamera.startPreview();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void surfaceCreated(SurfaceHolder holder) {
|
public void surfaceCreated(SurfaceHolder holder) {
|
||||||
camera = Camera.open();
|
mCamera = Camera.open();
|
||||||
camera.setPreviewCallback(
|
mCamera.setPreviewCallback(
|
||||||
new PreviewCallback() {
|
new PreviewCallback() {
|
||||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||||
synchronized(Sample0View.this)
|
synchronized(Sample0View.this) {
|
||||||
{
|
mFrame = data;
|
||||||
frame = data;
|
|
||||||
Sample0View.this.notify();
|
Sample0View.this.notify();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -71,11 +68,11 @@ class Sample0View extends SurfaceView implements SurfaceHolder.Callback, Runnabl
|
|||||||
|
|
||||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||||
mThreadRun = false;
|
mThreadRun = false;
|
||||||
if(camera != null) {
|
if(mCamera != null) {
|
||||||
camera.stopPreview();
|
mCamera.stopPreview();
|
||||||
camera.setPreviewCallback(null);
|
mCamera.setPreviewCallback(null);
|
||||||
camera.release();
|
mCamera.release();
|
||||||
camera = null;
|
mCamera = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -84,35 +81,31 @@ class Sample0View extends SurfaceView implements SurfaceHolder.Callback, Runnabl
|
|||||||
Log.i(TAG, "Starting thread");
|
Log.i(TAG, "Starting thread");
|
||||||
while(mThreadRun) {
|
while(mThreadRun) {
|
||||||
byte[] data = null;
|
byte[] data = null;
|
||||||
synchronized(this)
|
synchronized(this) {
|
||||||
{
|
|
||||||
try {
|
try {
|
||||||
this.wait();
|
this.wait();
|
||||||
data = frame;
|
data = mFrame;
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Canvas canvas = holder.lockCanvas();
|
int frameSize = mFrameWidth*mFrameHeight;
|
||||||
|
|
||||||
int frameSize = frame_width*frame_height;
|
|
||||||
int[] rgba = new int[frameSize];
|
int[] rgba = new int[frameSize];
|
||||||
|
|
||||||
Sample0Base a = (Sample0Base)getContext();
|
Sample0Base a = (Sample0Base)getContext();
|
||||||
int view_mode = a.view_mode;
|
int view_mode = a.viewMode;
|
||||||
if(view_mode == Sample0Base.view_mode_gray) {
|
if(view_mode == Sample0Base.VIEW_MODE_GRAY) {
|
||||||
for(int i = 0; i < frameSize; i++) {
|
for(int i = 0; i < frameSize; i++) {
|
||||||
int y = (0xff & ((int)data[i]));
|
int y = (0xff & ((int)data[i]));
|
||||||
rgba[i] = 0xff000000 + (y << 16) + (y << 8) + y;
|
rgba[i] = 0xff000000 + (y << 16) + (y << 8) + y;
|
||||||
}
|
}
|
||||||
}
|
} else if (view_mode == Sample0Base.VIEW_MODE_RGBA) {
|
||||||
else if (view_mode == Sample0Base.view_mode_rgba) {
|
for(int i = 0; i < mFrameHeight; i++)
|
||||||
for(int i = 0; i < frame_height; i++)
|
for(int j = 0; j < mFrameWidth; j++) {
|
||||||
for(int j = 0; j < frame_width; j++) {
|
int y = (0xff & ((int)data[i*mFrameWidth+j]));
|
||||||
int y = (0xff & ((int)data[i*frame_width+j]));
|
int u = (0xff & ((int)data[frameSize + (i >> 1) * mFrameWidth + (j & ~1) + 0]));
|
||||||
int u = (0xff & ((int)data[frameSize + (i >> 1) * frame_width + (j & ~1) + 0]));
|
int v = (0xff & ((int)data[frameSize + (i >> 1) * mFrameWidth + (j & ~1) + 1]));
|
||||||
int v = (0xff & ((int)data[frameSize + (i >> 1) * frame_width + (j & ~1) + 1]));
|
|
||||||
if (y < 16) y = 16;
|
if (y < 16) y = 16;
|
||||||
|
|
||||||
int r = Math.round(1.164f * (y - 16) + 1.596f * (v - 128) );
|
int r = Math.round(1.164f * (y - 16) + 1.596f * (v - 128) );
|
||||||
@ -123,15 +116,16 @@ class Sample0View extends SurfaceView implements SurfaceHolder.Callback, Runnabl
|
|||||||
if (g < 0) g = 0; if (g > 255) g = 255;
|
if (g < 0) g = 0; if (g > 255) g = 255;
|
||||||
if (b < 0) b = 0; if (b > 255) b = 255;
|
if (b < 0) b = 0; if (b > 255) b = 255;
|
||||||
|
|
||||||
rgba[i*frame_width+j] = 0xff000000 + (b << 16) + (g << 8) + r;
|
rgba[i*mFrameWidth+j] = 0xff000000 + (b << 16) + (g << 8) + r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Bitmap bmp = Bitmap.createBitmap(frame_width, frame_height, Bitmap.Config.ARGB_8888);
|
Bitmap bmp = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.ARGB_8888);
|
||||||
bmp.setPixels(rgba, 0/*offset*/, frame_width /*stride*/, 0, 0, frame_width, frame_height);
|
bmp.setPixels(rgba, 0/*offset*/, mFrameWidth /*stride*/, 0, 0, mFrameWidth, mFrameHeight);
|
||||||
|
|
||||||
canvas.drawBitmap(bmp, (canvas.getWidth()-frame_width)/2, (canvas.getHeight()-frame_height)/2, null);
|
Canvas canvas = mHolder.lockCanvas();
|
||||||
holder.unlockCanvasAndPost(canvas);
|
canvas.drawBitmap(bmp, (canvas.getWidth()-mFrameWidth)/2, (canvas.getHeight()-mFrameHeight)/2, null);
|
||||||
|
mHolder.unlockCanvasAndPost(canvas);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user