|
|
@ -2,6 +2,7 @@ package org.opencv.samples.tutorial4; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.List; |
|
|
|
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; |
|
|
@ -15,14 +16,14 @@ 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 = "Sample::SurfaceView"; |
|
|
|
private static final String TAG = "OCVSample::BaseView"; |
|
|
|
|
|
|
|
|
|
|
|
private Camera mCamera; |
|
|
|
private Camera mCamera; |
|
|
|
private SurfaceHolder mHolder; |
|
|
|
private SurfaceHolder mHolder; |
|
|
|
private int mFrameWidth; |
|
|
|
private int mFrameWidth; |
|
|
|
private int mFrameHeight; |
|
|
|
private int mFrameHeight; |
|
|
|
private byte[] mFrame; |
|
|
|
private byte[] mFrame; |
|
|
|
private boolean mThreadRun; |
|
|
|
private volatile boolean mThreadRun; |
|
|
|
private byte[] mBuffer; |
|
|
|
private byte[] mBuffer; |
|
|
|
private SurfaceTexture mSf; |
|
|
|
private SurfaceTexture mSf; |
|
|
|
|
|
|
|
|
|
|
@ -52,11 +53,29 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public boolean openCamera() { |
|
|
|
public boolean openCamera() { |
|
|
|
Log.i(TAG, "openCamera"); |
|
|
|
Log.i(TAG, "Opening Camera"); |
|
|
|
releaseCamera(); |
|
|
|
mCamera = null; |
|
|
|
mCamera = Camera.open(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
mCamera = Camera.open(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (Exception e){ |
|
|
|
|
|
|
|
Log.e(TAG, "Camera is not available (in use or does not exist): " + e.getLocalizedMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(mCamera == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) { |
|
|
|
|
|
|
|
for (int camIdx = 0; camIdx < Camera.getNumberOfCameras(); ++camIdx) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
mCamera = Camera.open(camIdx); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (RuntimeException e) { |
|
|
|
|
|
|
|
Log.e(TAG, "Camera #" + camIdx + "failed to open: " + e.getLocalizedMessage()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if(mCamera == null) { |
|
|
|
if(mCamera == null) { |
|
|
|
Log.e(TAG, "Can't open camera!"); |
|
|
|
Log.e(TAG, "Can't open any camera"); |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -69,16 +88,16 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde |
|
|
|
camera.addCallbackBuffer(mBuffer); |
|
|
|
camera.addCallbackBuffer(mBuffer); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void releaseCamera() { |
|
|
|
public void releaseCamera() { |
|
|
|
Log.i(TAG, "releaseCamera"); |
|
|
|
Log.i(TAG, "Releasing Camera"); |
|
|
|
mThreadRun = false; |
|
|
|
mThreadRun = false; |
|
|
|
synchronized (this) { |
|
|
|
synchronized (this) { |
|
|
|
if (mCamera != null) { |
|
|
|
if (mCamera != null) { |
|
|
|
mCamera.stopPreview(); |
|
|
|
mCamera.stopPreview(); |
|
|
|
mCamera.setPreviewCallback(null); |
|
|
|
|
|
|
|
mCamera.release(); |
|
|
|
mCamera.release(); |
|
|
|
mCamera = null; |
|
|
|
mCamera = null; |
|
|
|
} |
|
|
|
} |
|
|
@ -86,78 +105,81 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde |
|
|
|
onPreviewStopped(); |
|
|
|
onPreviewStopped(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void setupCamera(int width, int height) { |
|
|
|
public synchronized void setupCamera(int width, int height) { |
|
|
|
Log.i(TAG, "setupCamera"); |
|
|
|
if (mCamera != null) { |
|
|
|
synchronized (this) { |
|
|
|
Log.i(TAG, "Setup Camera - " + width + "x" + height); |
|
|
|
if (mCamera != null) { |
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
Camera.Parameters params = mCamera.getParameters(); |
|
|
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes(); |
|
|
|
List<Camera.Size> sizes = params.getSupportedPreviewSizes(); |
|
|
|
mFrameWidth = width; |
|
|
|
mFrameWidth = width; |
|
|
|
mFrameHeight = height; |
|
|
|
mFrameHeight = height; |
|
|
|
|
|
|
|
|
|
|
|
// selecting optimal camera preview size
|
|
|
|
// selecting optimal camera preview size
|
|
|
|
{ |
|
|
|
{ |
|
|
|
int minDiff = Integer.MAX_VALUE; |
|
|
|
int minDiff = Integer.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) { |
|
|
|
mFrameWidth = size.width; |
|
|
|
mFrameWidth = size.width; |
|
|
|
mFrameHeight = size.height; |
|
|
|
mFrameHeight = size.height; |
|
|
|
minDiff = Math.abs(size.height - height); |
|
|
|
minDiff = Math.abs(size.height - height); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
params.setPreviewSize(getFrameWidth(), getFrameHeight()); |
|
|
|
params.setPreviewSize(getFrameWidth(), getFrameHeight()); |
|
|
|
|
|
|
|
|
|
|
|
List<String> FocusModes = params.getSupportedFocusModes(); |
|
|
|
|
|
|
|
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mCamera.setParameters(params); |
|
|
|
List<String> FocusModes = params.getSupportedFocusModes(); |
|
|
|
|
|
|
|
if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* Now allocate the buffer */ |
|
|
|
mCamera.setParameters(params); |
|
|
|
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 copied */ |
|
|
|
|
|
|
|
mFrame = new byte [size]; |
|
|
|
|
|
|
|
mCamera.addCallbackBuffer(mBuffer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Notify that the preview is about to be started and deliver preview size */ |
|
|
|
/* Now allocate the buffer */ |
|
|
|
onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height); |
|
|
|
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 copied */ |
|
|
|
|
|
|
|
mFrame = new byte [size]; |
|
|
|
|
|
|
|
mCamera.addCallbackBuffer(mBuffer); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
/* Notify that the preview is about to be started and deliver preview size */ |
|
|
|
setPreview(); |
|
|
|
onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height); |
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Now we can start a preview */ |
|
|
|
try { |
|
|
|
mCamera.startPreview(); |
|
|
|
setPreview(); |
|
|
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
|
|
Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Now we can start a preview */ |
|
|
|
|
|
|
|
mCamera.startPreview(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) { |
|
|
|
public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) { |
|
|
|
Log.i(TAG, "surfaceChanged"); |
|
|
|
Log.i(TAG, "called surfaceChanged"); |
|
|
|
|
|
|
|
// stop preview before making changes
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
mCamera.stopPreview(); |
|
|
|
|
|
|
|
} catch (Exception e){ |
|
|
|
|
|
|
|
// ignore: tried to stop a non-existent preview
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// start preview with new settings
|
|
|
|
setupCamera(width, height); |
|
|
|
setupCamera(width, height); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void surfaceCreated(SurfaceHolder holder) { |
|
|
|
public void surfaceCreated(SurfaceHolder holder) { |
|
|
|
Log.i(TAG, "surfaceCreated"); |
|
|
|
Log.i(TAG, "called surfaceCreated"); |
|
|
|
(new Thread(this)).start(); |
|
|
|
(new Thread(this)).start(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void surfaceDestroyed(SurfaceHolder holder) { |
|
|
|
public void surfaceDestroyed(SurfaceHolder holder) { |
|
|
|
Log.i(TAG, "surfaceDestroyed"); |
|
|
|
Log.i(TAG, "called surfaceDestroyed"); |
|
|
|
releaseCamera(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */ |
|
|
|
/* The bitmap returned by this method shall be owned by the child and released in onPreviewStopped() */ |
|
|
|
protected abstract Bitmap processFrame(byte[] data); |
|
|
|
protected abstract Bitmap processFrame(byte[] data); |
|
|
|
|
|
|
|
|
|
|
@ -178,13 +200,15 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde |
|
|
|
|
|
|
|
|
|
|
|
public void run() { |
|
|
|
public void run() { |
|
|
|
mThreadRun = true; |
|
|
|
mThreadRun = true; |
|
|
|
Log.i(TAG, "Starting processing thread"); |
|
|
|
Log.i(TAG, "Started processing thread"); |
|
|
|
while (mThreadRun) { |
|
|
|
while (mThreadRun) { |
|
|
|
Bitmap bmp = null; |
|
|
|
Bitmap bmp = null; |
|
|
|
|
|
|
|
|
|
|
|
synchronized (this) { |
|
|
|
synchronized (this) { |
|
|
|
try { |
|
|
|
try { |
|
|
|
this.wait(); |
|
|
|
this.wait(); |
|
|
|
|
|
|
|
if (!mThreadRun) |
|
|
|
|
|
|
|
break; |
|
|
|
bmp = processFrame(mFrame); |
|
|
|
bmp = processFrame(mFrame); |
|
|
|
} catch (InterruptedException e) { |
|
|
|
} catch (InterruptedException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
e.printStackTrace(); |
|
|
@ -199,5 +223,6 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Log.i(TAG, "Finished processing thread"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |