mirror of
https://github.com/opencv/opencv.git
synced 2024-11-24 03:00:14 +08:00
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
|
@ -9,36 +9,36 @@ import android.view.Window;
|
||||
|
||||
public class Sample0Base extends Activity {
|
||||
private static final String TAG = "Sample0Base::Activity";
|
||||
|
||||
public static final int view_mode_rgba = 0;
|
||||
public static final int view_mode_gray = 1;
|
||||
|
||||
private MenuItem item_preview_rgba;
|
||||
private MenuItem item_preview_gray;
|
||||
|
||||
public int view_mode;
|
||||
|
||||
|
||||
public static final int VIEW_MODE_RGBA = 0;
|
||||
public static final int VIEW_MODE_GRAY = 1;
|
||||
|
||||
private MenuItem mItemPreviewRGBA;
|
||||
private MenuItem mItemPreviewGray;
|
||||
|
||||
public int viewMode;
|
||||
|
||||
/** Called when the activity is first created. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||||
setContentView( new Sample0View(this) );
|
||||
view_mode = view_mode_rgba;
|
||||
setContentView(new Sample0View(this));
|
||||
viewMode = VIEW_MODE_RGBA;
|
||||
}
|
||||
|
||||
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
item_preview_rgba = menu.add("Preview RGBA");
|
||||
item_preview_gray = menu.add("Preview GRAY");
|
||||
return true;
|
||||
mItemPreviewRGBA = menu.add("Preview RGBA");
|
||||
mItemPreviewGray = menu.add("Preview GRAY");
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean onOptionsItemSelected (MenuItem item) {
|
||||
Log.i(TAG, "Menu Item selected " + item);
|
||||
if (item == item_preview_rgba)
|
||||
view_mode = view_mode_rgba;
|
||||
else if (item == item_preview_gray)
|
||||
view_mode = view_mode_gray;
|
||||
return true;
|
||||
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
Log.i(TAG, "Menu Item selected " + item);
|
||||
if (item == mItemPreviewRGBA)
|
||||
viewMode = VIEW_MODE_RGBA;
|
||||
else if (item == mItemPreviewGray)
|
||||
viewMode = VIEW_MODE_GRAY;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package org.opencv.samples;
|
||||
|
||||
import java.util.List;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Canvas;
|
||||
@ -10,128 +9,123 @@ import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
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 Camera camera;
|
||||
private SurfaceHolder holder;
|
||||
private int frame_width;
|
||||
private int frame_height;
|
||||
private byte[] frame;
|
||||
|
||||
private Camera mCamera;
|
||||
private SurfaceHolder mHolder;
|
||||
private int mFrameWidth;
|
||||
private int mFrameHeight;
|
||||
private byte[] mFrame;
|
||||
private boolean mThreadRun;
|
||||
|
||||
|
||||
public Sample0View(Context context) {
|
||||
super(context);
|
||||
holder = getHolder();
|
||||
holder.addCallback(this);
|
||||
}
|
||||
|
||||
public Sample0View(Context context) {
|
||||
super(context);
|
||||
mHolder = getHolder();
|
||||
mHolder.addCallback(this);
|
||||
}
|
||||
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
if ( camera != null) {
|
||||
Camera.Parameters params = camera.getParameters();
|
||||
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) {
|
||||
if ( mCamera != null) {
|
||||
Camera.Parameters params = mCamera.getParameters();
|
||||
List<Camera.Size> sizes = params.getSupportedPreviewSizes();
|
||||
frame_width = width;
|
||||
frame_height = height;
|
||||
mFrameWidth = width;
|
||||
mFrameHeight = height;
|
||||
|
||||
//selecting optimal camera preview size
|
||||
//selecting optimal camera preview size
|
||||
{
|
||||
double minDiff = Double.MAX_VALUE;
|
||||
for (Camera.Size size : sizes) {
|
||||
if (Math.abs(size.height - height) < minDiff) {
|
||||
frame_width = size.width;
|
||||
frame_height = size.height;
|
||||
mFrameWidth = size.width;
|
||||
mFrameHeight = size.height;
|
||||
minDiff = Math.abs(size.height - height);
|
||||
}
|
||||
}
|
||||
}
|
||||
params.setPreviewSize(frame_width, frame_height);
|
||||
camera.setParameters(params);
|
||||
camera.startPreview();
|
||||
params.setPreviewSize(mFrameWidth, mFrameHeight);
|
||||
mCamera.setParameters(params);
|
||||
mCamera.startPreview();
|
||||
}
|
||||
}
|
||||
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
camera = Camera.open();
|
||||
camera.setPreviewCallback(
|
||||
new PreviewCallback() {
|
||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||
synchronized(Sample0View.this)
|
||||
{
|
||||
frame = data;
|
||||
Sample0View.this.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
(new Thread(this)).start();
|
||||
}
|
||||
public void surfaceCreated(SurfaceHolder holder) {
|
||||
mCamera = Camera.open();
|
||||
mCamera.setPreviewCallback(
|
||||
new PreviewCallback() {
|
||||
public void onPreviewFrame(byte[] data, Camera camera) {
|
||||
synchronized(Sample0View.this) {
|
||||
mFrame = data;
|
||||
Sample0View.this.notify();
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
(new Thread(this)).start();
|
||||
}
|
||||
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
mThreadRun = false;
|
||||
if(camera != null) {
|
||||
camera.stopPreview();
|
||||
camera.setPreviewCallback(null);
|
||||
camera.release();
|
||||
camera = null;
|
||||
}
|
||||
}
|
||||
public void surfaceDestroyed(SurfaceHolder holder) {
|
||||
mThreadRun = false;
|
||||
if(mCamera != null) {
|
||||
mCamera.stopPreview();
|
||||
mCamera.setPreviewCallback(null);
|
||||
mCamera.release();
|
||||
mCamera = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void run() {
|
||||
mThreadRun = true;
|
||||
Log.i(TAG, "Starting thread");
|
||||
while(mThreadRun) {
|
||||
byte[] data = null;
|
||||
synchronized(this)
|
||||
{
|
||||
try {
|
||||
this.wait();
|
||||
data = frame;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
Canvas canvas = holder.lockCanvas();
|
||||
|
||||
int frameSize = frame_width*frame_height;
|
||||
int[] rgba = new int[frameSize];
|
||||
|
||||
Sample0Base a = (Sample0Base)getContext();
|
||||
int view_mode = a.view_mode;
|
||||
if(view_mode == Sample0Base.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) {
|
||||
for(int i = 0; i < frame_height; i++)
|
||||
for(int j = 0; j < frame_width; j++) {
|
||||
int y = (0xff & ((int)data[i*frame_width+j]));
|
||||
int u = (0xff & ((int)data[frameSize + (i >> 1) * frame_width + (j & ~1) + 0]));
|
||||
int v = (0xff & ((int)data[frameSize + (i >> 1) * frame_width + (j & ~1) + 1]));
|
||||
if (y < 16) y = 16;
|
||||
|
||||
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));
|
||||
|
||||
if (r < 0) r = 0; if (r > 255) r = 255;
|
||||
if (g < 0) g = 0; if (g > 255) g = 255;
|
||||
if (b < 0) b = 0; if (b > 255) b = 255;
|
||||
|
||||
rgba[i*frame_width+j] = 0xff000000 + (b << 16) + (g << 8) + r;
|
||||
}
|
||||
}
|
||||
|
||||
Bitmap bmp = Bitmap.createBitmap(frame_width, frame_height, Bitmap.Config.ARGB_8888);
|
||||
bmp.setPixels(rgba, 0/*offset*/, frame_width /*stride*/, 0, 0, frame_width, frame_height);
|
||||
|
||||
canvas.drawBitmap(bmp, (canvas.getWidth()-frame_width)/2, (canvas.getHeight()-frame_height)/2, null);
|
||||
holder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
}
|
||||
public void run() {
|
||||
mThreadRun = true;
|
||||
Log.i(TAG, "Starting thread");
|
||||
while(mThreadRun) {
|
||||
byte[] data = null;
|
||||
synchronized(this) {
|
||||
try {
|
||||
this.wait();
|
||||
data = mFrame;
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
int frameSize = mFrameWidth*mFrameHeight;
|
||||
int[] rgba = new int[frameSize];
|
||||
|
||||
Sample0Base a = (Sample0Base)getContext();
|
||||
int view_mode = a.viewMode;
|
||||
if(view_mode == Sample0Base.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) {
|
||||
for(int i = 0; i < mFrameHeight; i++)
|
||||
for(int j = 0; j < mFrameWidth; j++) {
|
||||
int y = (0xff & ((int)data[i*mFrameWidth+j]));
|
||||
int u = (0xff & ((int)data[frameSize + (i >> 1) * mFrameWidth + (j & ~1) + 0]));
|
||||
int v = (0xff & ((int)data[frameSize + (i >> 1) * mFrameWidth + (j & ~1) + 1]));
|
||||
if (y < 16) y = 16;
|
||||
|
||||
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));
|
||||
|
||||
if (r < 0) r = 0; if (r > 255) r = 255;
|
||||
if (g < 0) g = 0; if (g > 255) g = 255;
|
||||
if (b < 0) b = 0; if (b > 255) b = 255;
|
||||
|
||||
rgba[i*mFrameWidth+j] = 0xff000000 + (b << 16) + (g << 8) + r;
|
||||
}
|
||||
}
|
||||
|
||||
Bitmap bmp = Bitmap.createBitmap(mFrameWidth, mFrameHeight, Bitmap.Config.ARGB_8888);
|
||||
bmp.setPixels(rgba, 0/*offset*/, mFrameWidth /*stride*/, 0, 0, mFrameWidth, mFrameHeight);
|
||||
|
||||
Canvas canvas = mHolder.lockCanvas();
|
||||
canvas.drawBitmap(bmp, (canvas.getWidth()-mFrameWidth)/2, (canvas.getHeight()-mFrameHeight)/2, null);
|
||||
mHolder.unlockCanvasAndPost(canvas);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user