|
@@ -1,571 +0,0 @@
|
|
|
-package com.miekir.ocr.ui;
|
|
|
|
|
-
|
|
|
|
|
-import android.Manifest;
|
|
|
|
|
-import android.annotation.SuppressLint;
|
|
|
|
|
-import android.content.Context;
|
|
|
|
|
-import android.content.pm.PackageManager;
|
|
|
|
|
-import android.content.res.Configuration;
|
|
|
|
|
-import android.graphics.ImageFormat;
|
|
|
|
|
-import android.graphics.Point;
|
|
|
|
|
-import android.graphics.SurfaceTexture;
|
|
|
|
|
-import android.hardware.camera2.CameraAccessException;
|
|
|
|
|
-import android.hardware.camera2.CameraCaptureSession;
|
|
|
|
|
-import android.hardware.camera2.CameraCharacteristics;
|
|
|
|
|
-import android.hardware.camera2.CameraDevice;
|
|
|
|
|
-import android.hardware.camera2.CameraManager;
|
|
|
|
|
-import android.hardware.camera2.CameraMetadata;
|
|
|
|
|
-import android.hardware.camera2.CaptureRequest;
|
|
|
|
|
-import android.hardware.camera2.TotalCaptureResult;
|
|
|
|
|
-import android.hardware.camera2.params.StreamConfigurationMap;
|
|
|
|
|
-import android.media.Image;
|
|
|
|
|
-import android.media.ImageReader;
|
|
|
|
|
-import android.os.Build;
|
|
|
|
|
-import android.os.Bundle;
|
|
|
|
|
-import android.os.Environment;
|
|
|
|
|
-import android.os.Handler;
|
|
|
|
|
-import android.os.HandlerThread;
|
|
|
|
|
-import android.util.Log;
|
|
|
|
|
-import android.util.Range;
|
|
|
|
|
-import android.util.Size;
|
|
|
|
|
-import android.util.SparseIntArray;
|
|
|
|
|
-import android.view.Surface;
|
|
|
|
|
-import android.view.TextureView;
|
|
|
|
|
-import android.widget.ImageView;
|
|
|
|
|
-
|
|
|
|
|
-import androidx.annotation.NonNull;
|
|
|
|
|
-
|
|
|
|
|
-import com.miekir.ocr.R;
|
|
|
|
|
-import com.miekir.ocr.base.BaseCameraActivity;
|
|
|
|
|
-import com.miekir.ocr.tool.CameraPreviewTool;
|
|
|
|
|
-import com.miekir.ocr.tool.ImageUtil;
|
|
|
|
|
-import com.miekir.ocr.widget.AutoFitTextureView;
|
|
|
|
|
-
|
|
|
|
|
-import java.io.File;
|
|
|
|
|
-import java.io.FileOutputStream;
|
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
-import java.io.OutputStream;
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
|
|
-import java.util.Arrays;
|
|
|
|
|
-import java.util.Collections;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-import java.util.concurrent.Semaphore;
|
|
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
|
|
-
|
|
|
|
|
-public class CameraActivity extends BaseCameraActivity {
|
|
|
|
|
- protected int mLeft;
|
|
|
|
|
- protected int mTop;
|
|
|
|
|
- protected int mRight;
|
|
|
|
|
- protected int mBottom;
|
|
|
|
|
-
|
|
|
|
|
- private CameraManager mCameraManager;
|
|
|
|
|
-
|
|
|
|
|
- private static SparseIntArray ORIENTATIONS = new SparseIntArray();
|
|
|
|
|
-
|
|
|
|
|
- static {
|
|
|
|
|
- ORIENTATIONS.append(Surface.ROTATION_0, 90);
|
|
|
|
|
- ORIENTATIONS.append(Surface.ROTATION_90, 0);
|
|
|
|
|
- ORIENTATIONS.append(Surface.ROTATION_180, 270);
|
|
|
|
|
- ORIENTATIONS.append(Surface.ROTATION_270, 180);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private String cameraId;
|
|
|
|
|
- private CameraDevice cameraDevice;
|
|
|
|
|
- private CameraCaptureSession cameraCaptureSessions;
|
|
|
|
|
- private CaptureRequest.Builder captureRequestBuilder;
|
|
|
|
|
- private Size imageDimension;
|
|
|
|
|
- private ImageReader imageReader;
|
|
|
|
|
-
|
|
|
|
|
- private File directoryFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/");
|
|
|
|
|
-
|
|
|
|
|
- private Handler mBackgroundHandler;
|
|
|
|
|
- private HandlerThread mBackgroundThread;
|
|
|
|
|
-
|
|
|
|
|
- private AutoFitTextureView textureView;
|
|
|
|
|
- private boolean mIsFlashOpen = false;
|
|
|
|
|
- private ImageView iv_flash;
|
|
|
|
|
-
|
|
|
|
|
- private Semaphore mCameraOpenCloseLock = new Semaphore(1);
|
|
|
|
|
-
|
|
|
|
|
- private static final int MAX_PREVIEW_WIDTH = 1920;
|
|
|
|
|
- private static final int MAX_PREVIEW_HEIGHT = 1080;
|
|
|
|
|
- private int mSensorOrientation;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- private TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
|
|
|
|
|
- openCamera(width, height);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
|
|
|
|
|
- if (null != textureView || null == imageDimension) {
|
|
|
|
|
- textureView.setTransform(CameraPreviewTool.configureTransform(width, height, imageDimension, CameraActivity.this));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onSurfaceTextureUpdated(SurfaceTexture surface) {
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- private CameraDevice.StateCallback stateCallback = new CameraDevice.StateCallback() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onOpened(CameraDevice camera) {
|
|
|
|
|
-
|
|
|
|
|
- Log.e("tag", "onOpened");
|
|
|
|
|
- mCameraOpenCloseLock.release();
|
|
|
|
|
- cameraDevice = camera;
|
|
|
|
|
- createCameraPreview();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onDisconnected(CameraDevice camera) {
|
|
|
|
|
- mCameraOpenCloseLock.release();
|
|
|
|
|
- cameraDevice.close();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onError(CameraDevice camera, int error) {
|
|
|
|
|
- mCameraOpenCloseLock.release();
|
|
|
|
|
- cameraDevice.close();
|
|
|
|
|
- cameraDevice = null;
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public int getLayoutID() {
|
|
|
|
|
- return R.layout.activity_camera;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- @SuppressLint("CheckResult")
|
|
|
|
|
- @Override
|
|
|
|
|
- public void initViews(Bundle savedInstanceState) {
|
|
|
|
|
- textureView = findViewById(R.id.texture);
|
|
|
|
|
- textureView.setSurfaceTextureListener(textureListener);
|
|
|
|
|
- iv_flash = findViewById(R.id.iv_flash);
|
|
|
|
|
- iv_flash.setOnClickListener(v -> {
|
|
|
|
|
- mIsFlashOpen = !mIsFlashOpen;
|
|
|
|
|
- if (mIsFlashOpen) {
|
|
|
|
|
- iv_flash.setImageResource(R.mipmap.flash_open);
|
|
|
|
|
- } else {
|
|
|
|
|
- iv_flash.setImageResource(R.mipmap.flash_close);
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- textureView.post(new Runnable() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void run() {
|
|
|
|
|
- setWidthHeight(textureView.getWidth(), textureView.getHeight());
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- protected void setWidthHeight(int width, int height) {
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- private void openCamera(int width, int height) {
|
|
|
|
|
- mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
|
|
|
|
|
- Log.e("tag", "is camera open");
|
|
|
|
|
-
|
|
|
|
|
- try {
|
|
|
|
|
- if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
|
|
|
|
|
- throw new RuntimeException("Time out waiting to lock camera opening.");
|
|
|
|
|
- }
|
|
|
|
|
- cameraId = mCameraManager.getCameraIdList()[0];
|
|
|
|
|
- CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(cameraId);
|
|
|
|
|
- StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
|
|
|
|
|
- assert map != null;
|
|
|
|
|
-
|
|
|
|
|
- Size largest = Collections.max(
|
|
|
|
|
- Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
|
|
|
|
|
- new CameraPreviewTool.CompareSizesByArea());
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- int displayRotation = getWindowManager().getDefaultDisplay().getRotation();
|
|
|
|
|
- //noinspection ConstantConditions
|
|
|
|
|
- mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
|
|
|
|
|
- boolean swappedDimensions = false;
|
|
|
|
|
- switch (displayRotation) {
|
|
|
|
|
- case Surface.ROTATION_0:
|
|
|
|
|
- case Surface.ROTATION_180:
|
|
|
|
|
- if (mSensorOrientation == 90 || mSensorOrientation == 270) {
|
|
|
|
|
- swappedDimensions = true;
|
|
|
|
|
- }
|
|
|
|
|
- break;
|
|
|
|
|
- case Surface.ROTATION_90:
|
|
|
|
|
- case Surface.ROTATION_270:
|
|
|
|
|
- if (mSensorOrientation == 0 || mSensorOrientation == 180) {
|
|
|
|
|
- swappedDimensions = true;
|
|
|
|
|
- }
|
|
|
|
|
- break;
|
|
|
|
|
- default:
|
|
|
|
|
- Log.e("tag", "Display rotation is invalid: " + displayRotation);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- Point displaySize = new Point();
|
|
|
|
|
- getWindowManager().getDefaultDisplay().getSize(displaySize);
|
|
|
|
|
- int rotatedPreviewWidth = width;
|
|
|
|
|
- int rotatedPreviewHeight = height;
|
|
|
|
|
- int maxPreviewWidth = displaySize.x;
|
|
|
|
|
- int maxPreviewHeight = displaySize.y;
|
|
|
|
|
-
|
|
|
|
|
- if (swappedDimensions) {
|
|
|
|
|
- rotatedPreviewWidth = height;
|
|
|
|
|
- rotatedPreviewHeight = width;
|
|
|
|
|
- maxPreviewWidth = displaySize.y;
|
|
|
|
|
- maxPreviewHeight = displaySize.x;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (maxPreviewWidth > MAX_PREVIEW_WIDTH) {
|
|
|
|
|
- maxPreviewWidth = MAX_PREVIEW_WIDTH;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (maxPreviewHeight > MAX_PREVIEW_HEIGHT) {
|
|
|
|
|
- maxPreviewHeight = MAX_PREVIEW_HEIGHT;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- imageDimension = CameraPreviewTool.chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class),
|
|
|
|
|
- rotatedPreviewWidth, rotatedPreviewHeight, maxPreviewWidth,
|
|
|
|
|
- maxPreviewHeight, largest);
|
|
|
|
|
-
|
|
|
|
|
- int orientation = getResources().getConfiguration().orientation;
|
|
|
|
|
- if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
|
|
|
|
|
- textureView.setAspectRatio(
|
|
|
|
|
- imageDimension.getWidth(), imageDimension.getHeight());
|
|
|
|
|
- } else {
|
|
|
|
|
- textureView.setAspectRatio(
|
|
|
|
|
- imageDimension.getHeight(), imageDimension.getWidth());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (null != textureView || null == imageDimension) {
|
|
|
|
|
- textureView.setTransform(CameraPreviewTool.configureTransform(width, height, imageDimension, CameraActivity.this));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
|
|
|
|
|
- checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- mCameraManager.openCamera(cameraId, stateCallback, null);
|
|
|
|
|
- } catch (CameraAccessException | InterruptedException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- Log.e("tag", "openCamera X");
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private Range<Integer> getRange() {
|
|
|
|
|
- CameraCharacteristics chars = null;
|
|
|
|
|
- try {
|
|
|
|
|
- chars = mCameraManager.getCameraCharacteristics(cameraId);
|
|
|
|
|
- Range<Integer>[] ranges = chars.get(CameraCharacteristics.CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES);
|
|
|
|
|
- Range<Integer> result = null;
|
|
|
|
|
- for (Range<Integer> range : ranges) {
|
|
|
|
|
- int upper = range.getUpper();
|
|
|
|
|
- // 10 - min range upper for my needs
|
|
|
|
|
- if (upper >= 10) {
|
|
|
|
|
- if (result == null || upper < result.getUpper().intValue()) {
|
|
|
|
|
- result = range;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- if (result == null) {
|
|
|
|
|
- result = ranges[0];
|
|
|
|
|
- }
|
|
|
|
|
- return result;
|
|
|
|
|
- } catch (CameraAccessException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected void createCameraPreview() {
|
|
|
|
|
- try {
|
|
|
|
|
- SurfaceTexture texture = textureView.getSurfaceTexture();
|
|
|
|
|
- assert texture != null;
|
|
|
|
|
- texture.setDefaultBufferSize(imageDimension.getWidth(), imageDimension.getHeight());
|
|
|
|
|
- Surface surface = new Surface(texture);
|
|
|
|
|
- captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
|
|
|
|
|
- //captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
|
|
|
|
|
- // 解决预览太暗的问题
|
|
|
|
|
- captureRequestBuilder.set(CaptureRequest.CONTROL_AE_TARGET_FPS_RANGE, getRange());
|
|
|
|
|
- captureRequestBuilder.addTarget(surface);
|
|
|
|
|
-
|
|
|
|
|
- cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onConfigured(@NonNull CameraCaptureSession cameraCaptureSession) {
|
|
|
|
|
- //The camera is already closed
|
|
|
|
|
- if (null == cameraDevice) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- // When the session is ready, we start displaying the preview.
|
|
|
|
|
- cameraCaptureSessions = cameraCaptureSession;
|
|
|
|
|
- updatePreview();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onConfigureFailed(@NonNull CameraCaptureSession cameraCaptureSession) {
|
|
|
|
|
- //Toast.makeText(CameraActivity.this, "Configuration change", Toast.LENGTH_SHORT).show();
|
|
|
|
|
- }
|
|
|
|
|
- }, null);
|
|
|
|
|
- } catch (CameraAccessException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected void updatePreview() {
|
|
|
|
|
- if (null == cameraDevice) {
|
|
|
|
|
- Log.e("tag", "updatePreview error, return");
|
|
|
|
|
- }
|
|
|
|
|
- captureRequestBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
|
|
|
|
|
- //justFlashLight(captureRequestBuilder);
|
|
|
|
|
- try {
|
|
|
|
|
- cameraCaptureSessions.setRepeatingRequest(captureRequestBuilder.build(), null, mBackgroundHandler);
|
|
|
|
|
- } catch (CameraAccessException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected void startBackgroundThread() {
|
|
|
|
|
- mBackgroundThread = new HandlerThread("Camera Background");
|
|
|
|
|
- mBackgroundThread.start();
|
|
|
|
|
- mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected void stopBackgroundThread() {
|
|
|
|
|
- mBackgroundThread.quitSafely();
|
|
|
|
|
- try {
|
|
|
|
|
- mBackgroundThread.join();
|
|
|
|
|
- mBackgroundThread = null;
|
|
|
|
|
- mBackgroundHandler = null;
|
|
|
|
|
- } catch (InterruptedException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private void closeCamera() {
|
|
|
|
|
- try {
|
|
|
|
|
- mCameraOpenCloseLock.acquire();
|
|
|
|
|
- if (null != cameraCaptureSessions) {
|
|
|
|
|
- cameraCaptureSessions.close();
|
|
|
|
|
- cameraCaptureSessions = null;
|
|
|
|
|
- }
|
|
|
|
|
- if (null != cameraDevice) {
|
|
|
|
|
- cameraDevice.close();
|
|
|
|
|
- cameraDevice = null;
|
|
|
|
|
- }
|
|
|
|
|
- if (null != imageReader) {
|
|
|
|
|
- imageReader.close();
|
|
|
|
|
- imageReader = null;
|
|
|
|
|
- }
|
|
|
|
|
- } catch (InterruptedException e) {
|
|
|
|
|
- throw new RuntimeException("Interrupted while trying to lock camera closing.");
|
|
|
|
|
- } finally {
|
|
|
|
|
- mCameraOpenCloseLock.release();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- protected void onResume() {
|
|
|
|
|
- super.onResume();
|
|
|
|
|
- Log.e("tag", "onResume");
|
|
|
|
|
- startBackgroundThread();
|
|
|
|
|
- if (textureView.isAvailable()) {
|
|
|
|
|
- openCamera(textureView.getWidth(), textureView.getHeight());
|
|
|
|
|
- } else {
|
|
|
|
|
- textureView.setSurfaceTextureListener(textureListener);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- protected void onPause() {
|
|
|
|
|
- Log.e("tag", "onPause");
|
|
|
|
|
- closeCamera();
|
|
|
|
|
- stopBackgroundThread();
|
|
|
|
|
- super.onPause();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected void takePicture() {
|
|
|
|
|
- if (null == cameraDevice) {
|
|
|
|
|
- Log.e("tag", "cameraDevice is null");
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
|
|
|
|
|
- try {
|
|
|
|
|
- CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
|
|
|
|
|
- Size[] jpegSizes = null;
|
|
|
|
|
- if (characteristics != null) {
|
|
|
|
|
- jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);
|
|
|
|
|
- }
|
|
|
|
|
- int width = 640;
|
|
|
|
|
- int height = 480;
|
|
|
|
|
- if (jpegSizes != null && 0 < jpegSizes.length) {
|
|
|
|
|
- width = jpegSizes[0].getWidth();
|
|
|
|
|
- height = jpegSizes[0].getHeight();
|
|
|
|
|
- }
|
|
|
|
|
- //ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 1);
|
|
|
|
|
- // 解决拍照慢的问题
|
|
|
|
|
- ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.YUV_420_888, 1);
|
|
|
|
|
- List<Surface> outputSurfaces = new ArrayList<Surface>(2);
|
|
|
|
|
- outputSurfaces.add(reader.getSurface());
|
|
|
|
|
- outputSurfaces.add(new Surface(textureView.getSurfaceTexture()));
|
|
|
|
|
- final CaptureRequest.Builder captureBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE);
|
|
|
|
|
- captureBuilder.addTarget(reader.getSurface());
|
|
|
|
|
- captureBuilder.set(CaptureRequest.CONTROL_MODE, CameraMetadata.CONTROL_MODE_AUTO);
|
|
|
|
|
- // Orientation
|
|
|
|
|
- int rotation = getWindowManager().getDefaultDisplay().getRotation();
|
|
|
|
|
- captureBuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATIONS.get(rotation));
|
|
|
|
|
- reader.setOnImageAvailableListener(readerListener, mBackgroundHandler);
|
|
|
|
|
-
|
|
|
|
|
- final CameraCaptureSession.CaptureCallback captureListener = new CameraCaptureSession.CaptureCallback() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
|
|
|
|
|
- super.onCaptureCompleted(session, request, result);
|
|
|
|
|
- //Toast.makeText(CameraActivity.this, "Saved:" + directoryFile, Toast.LENGTH_SHORT).show();
|
|
|
|
|
- createCameraPreview();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
- justFlashLight(captureBuilder);
|
|
|
|
|
- cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onConfigured(CameraCaptureSession session) {
|
|
|
|
|
- try {
|
|
|
|
|
- session.capture(captureBuilder.build(), captureListener, mBackgroundHandler);
|
|
|
|
|
- } catch (CameraAccessException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onConfigureFailed(CameraCaptureSession session) {
|
|
|
|
|
- }
|
|
|
|
|
- }, mBackgroundHandler);
|
|
|
|
|
- } catch (CameraAccessException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private void justFlashLight(CaptureRequest.Builder captureBuilder) {
|
|
|
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
|
|
|
- if (mIsFlashOpen) {
|
|
|
|
|
- captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
|
|
|
|
|
- // 解决在部分手机全黑的问题
|
|
|
|
|
- captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
|
|
|
|
|
- } else {
|
|
|
|
|
- captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
|
|
|
|
|
- captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // 只有摄像头关闭的时候,才能用以下的方法
|
|
|
|
|
- /*try {
|
|
|
|
|
- //获取CameraManager
|
|
|
|
|
- CameraManager mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
|
|
|
|
|
- //获取当前手机所有摄像头设备ID
|
|
|
|
|
- String[] ids = mCameraManager.getCameraIdList();
|
|
|
|
|
- for (String id : ids) {
|
|
|
|
|
- CameraCharacteristics c = mCameraManager.getCameraCharacteristics(id);
|
|
|
|
|
- //查询该摄像头组件是否包含闪光灯
|
|
|
|
|
- Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
|
|
|
|
|
- Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
|
|
|
|
|
- if (flashAvailable != null && flashAvailable
|
|
|
|
|
- && lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
|
|
|
|
|
- //打开或关闭手电筒
|
|
|
|
|
- mCameraManager.setTorchMode(id, true);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- } catch (CameraAccessException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- }*/
|
|
|
|
|
- } else {
|
|
|
|
|
- if (mIsFlashOpen) {
|
|
|
|
|
- // 拍完关闭闪光灯
|
|
|
|
|
- captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_SINGLE);
|
|
|
|
|
- //captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
|
|
|
|
|
- captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
|
|
|
|
|
- } else {
|
|
|
|
|
- captureBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
|
|
|
|
|
- //captureRequestBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
|
|
|
|
|
- captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- ImageReader.OnImageAvailableListener readerListener = new ImageReader.OnImageAvailableListener() {
|
|
|
|
|
- @Override
|
|
|
|
|
- public void onImageAvailable(ImageReader reader) {
|
|
|
|
|
-
|
|
|
|
|
- // YUV_420_888转换成jpg
|
|
|
|
|
- Image image = reader.acquireLatestImage();
|
|
|
|
|
- if (image != null) {
|
|
|
|
|
- //converting to JPEG
|
|
|
|
|
- byte[] jpegData = ImageUtil.imageToByteArray(image);
|
|
|
|
|
-
|
|
|
|
|
- // 写入到文件
|
|
|
|
|
- //FileManager.writeFrame(directoryFile.getAbsolutePath(), jpegData);
|
|
|
|
|
- image.close();
|
|
|
|
|
-
|
|
|
|
|
- // 得到裁剪后的图片数据
|
|
|
|
|
- int[] rectData = new int[]{mLeft, mTop, mRight, mBottom};
|
|
|
|
|
-
|
|
|
|
|
- if (!directoryFile.exists()) {
|
|
|
|
|
- directoryFile.mkdirs();
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- File file = new File(directoryFile, System.currentTimeMillis() + ".jpg");
|
|
|
|
|
- onCropFinish(jpegData, rectData, file);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /*Image image = null;
|
|
|
|
|
- try {
|
|
|
|
|
- image = reader.acquireLatestImage();
|
|
|
|
|
- ByteBuffer buffer = image.getPlanes()[0].getBuffer();
|
|
|
|
|
- byte[] bytes = new byte[buffer.capacity()];
|
|
|
|
|
- buffer.get(bytes);
|
|
|
|
|
- save(bytes);
|
|
|
|
|
- } catch (FileNotFoundException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- } catch (IOException e) {
|
|
|
|
|
- e.printStackTrace();
|
|
|
|
|
- } finally {
|
|
|
|
|
- if (image != null) {
|
|
|
|
|
- image.close();
|
|
|
|
|
- }
|
|
|
|
|
- }*/
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private void save(byte[] bytes) throws IOException {
|
|
|
|
|
- OutputStream output = null;
|
|
|
|
|
- try {
|
|
|
|
|
- output = new FileOutputStream(directoryFile);
|
|
|
|
|
- output.write(bytes);
|
|
|
|
|
- } finally {
|
|
|
|
|
- if (null != output) {
|
|
|
|
|
- output.close();
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- };
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * 裁剪完成,上传识别
|
|
|
|
|
- */
|
|
|
|
|
- protected void onCropFinish(byte[] rawPhotoBytes, int[] rectData, File file) {
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|