Реализую приложение, которое должно получать изображение с камеры на обработку 30 раз в секунду. Как лучше реализовать? Не думаю, что можно просто делать фото.
1 ответ
Базовый класс для работы с камерой, который я использовал, целиком:
public abstract class BaseCameraActivity extends BaseResearchActivity {
protected static final String TAG = "Fit-Camera";
protected SurfaceView myVideoCaptureView;
protected Camera myCamera;
protected abstract void initActionDetector();
protected void initCameraCaptureSurface(int id) {
myVideoCaptureView = (SurfaceView) findViewById(id);
SurfaceHolder videoCaptureViewHolder = myVideoCaptureView.getHolder();
if (videoCaptureViewHolder == null) {
Log.e(TAG, "Camera cannot be captured");
return;
}
// setPreviewDisplay() does not work without this line on my old HTC Desire HD.
// HTC One SV works perfectly.
videoCaptureViewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
videoCaptureViewHolder.addCallback(new SurfaceHolder.Callback() {
public void surfaceDestroyed(SurfaceHolder holder) {
stopVideo();
}
public void surfaceCreated(SurfaceHolder holder) {
}
public void surfaceChanged(SurfaceHolder holder, int format,
int width, int height) {
startVideo();
}
});
}
protected void startVideo() {
if (!prepareCamera()) {
return;
}
initActionDetector();
startPreview();
}
protected boolean prepareCamera() {
try {
myCamera = openFrontFacingCamera();
if (myCamera == null) {
return false;
}
SurfaceHolder videoCaptureViewHolder = (myVideoCaptureView != null) ? myVideoCaptureView.getHolder() : null;
myCamera.setPreviewDisplay(videoCaptureViewHolder);
myCamera.setDisplayOrientation(90); // http://stackoverflow.com/questions/3841122/android-camera-preview-is-sideways
Camera.Parameters parameters = myCamera.getParameters();
parameters.setPreviewFrameRate(30);
parameters.setPreviewFpsRange(15000, 30000);
parameters.setPreviewSize(320, 240);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
parameters.setColorEffect(Camera.Parameters.EFFECT_MONO);
myCamera.setParameters(parameters);
logSupportedParameters(parameters);
return true;
} catch (Throwable throwable) {
Log.e(TAG, throwable.getLocalizedMessage(), throwable);
return false;
}
}
protected void startPreview() {
try {
myCamera.startPreview();
} catch (Throwable throwable) {
Log.e(TAG, throwable.getLocalizedMessage(), throwable);
myCamera.release();
myCamera = null;
}
}
private void logSupportedParameters(@NotNull Camera.Parameters parameters) {
if (!DebugMode.DEBUG_LOG_ON) {
return;
}
List<int[]> supportedPreviewFps = parameters.getSupportedPreviewFpsRange();
if (supportedPreviewFps != null) {
for (int[] tmpRate : supportedPreviewFps) {
StringBuilder builder = new StringBuilder();
builder.append("supportedPreviewRate: ");
for (int i = tmpRate.length, j = 0; j < i; j++) {
builder.append(tmpRate[j]).append(", ");
}
Log.d(TAG, builder.toString());
}
}
List<Camera.Size> supportedPreviewSizes = parameters.getSupportedPreviewSizes();
if (supportedPreviewSizes != null) {
for (Camera.Size size : supportedPreviewSizes) {
Log.d(TAG, "supportedPreviewSize.width = " + size.width + ", supportedPreviewSize.height = " + size.height);
}
}
Log.d(TAG, "previewFrameRate=" + parameters.getPreviewFrameRate());
}
// http://stackoverflow.com/questions/2779002/how-to-open-front-camera-on-android-platform
@Nullable
private static Camera openFrontFacingCamera() {
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
int cameraCount = Camera.getNumberOfCameras();
if (DebugMode.DEBUG_LOG_ON) {
Log.i(TAG, "getNumberOfCameras=" + cameraCount);
}
for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
Camera.getCameraInfo(camIdx, cameraInfo);
if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
return Camera.open(camIdx);
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
try {
return Camera.open();
} catch (RuntimeException e) {
Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
return null;
}
}
private void stopVideo() {
if (myCamera == null) {
return;
}
try {
myCamera.stopPreview();
myCamera.setPreviewDisplay(null);
myCamera.setPreviewCallbackWithBuffer(null);
myCamera.release();
} catch (Throwable throwable) {
Log.e(TAG, throwable.getLocalizedMessage(), throwable);
} finally {
myCamera = null;
}
}
public void finish() {
stopVideo();
super.finish();
}
protected void setFrame(int id, @NotNull Bitmap bitmap) {
ImageView imageView = (ImageView) findViewById(id);
Bitmap copyBitmap = bitmap.copy(Bitmap.Config.RGB_565, false);
imageView.setImageBitmap(copyBitmap);
}
}
Мне нужна была front-facing камера, если есть, поэтому внутри именно эта константа. Ровно также можно получить основную камеру.