使用camera2 api捕获图像

时间:2018-05-29 05:32:18

标签: android android-camera android-camera2

我在一个应用程序中工作,我需要在LANDSCAPE模式下捕获图像。功能正常,但我得到的问题是,当捕获图像时,我正在获取捕获图像的额外一面。当我使用变换方法时,我遇到了这个问题(代码如下)。请告诉我如何删除图像的额外一面。请看下面的图片

1)捕捉图像之前
enter image description here

2)捕获图像后 enter image description here

相机横向模式代码

private void transformImage(int viewWidth, int viewHeight) {
    if (null == textureView || null == previewSize) {
        return;
    }
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    matrix = new Matrix();
    RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
    RectF bufferRect = new RectF(0, 0, previewSize.getHeight(), previewSize.getWidth());
    float centerX = viewRect.centerX();
    float centerY = viewRect.centerY();
    if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
        bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
        matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
        float scale = Math.max(
                (float) viewHeight / previewSize.getHeight(),
                (float) viewWidth / previewSize.getWidth());
        matrix.postScale(scale, scale, centerX, centerY);
        matrix.postRotate(90 * (rotation - 2), centerX, centerY);
    } else if (Surface.ROTATION_180 == rotation) {
        matrix.postRotate(180, centerX, centerY);
    }
    textureView.setTransform(matrix);
}

捕获图像的代码

void getPicture() {
    if (cameraDevice == null) {
        return;
    }
    CameraManager manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        assert manager != null;
        CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraDevice.getId());
        if (characteristics != null) {
     jpegSizes = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP).getOutputSizes(ImageFormat.JPEG);
        }
        int width = 640, height = 480;
        if (jpegSizes != null && jpegSizes.length > 0) {

            width = jpegSizes[0].getWidth();
            height = jpegSizes[0].getHeight();

        }
        final ImageReader reader = ImageReader.newInstance(width, height, ImageFormat.JPEG, 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);
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        capturebuilder.set(CaptureRequest.JPEG_ORIENTATION, ORIENTATION.get(rotation));



        ImageReader.OnImageAvailableListener imageAvailableListener = new ImageReader.OnImageAvailableListener() {
            @Override
            public void onImageAvailable(ImageReader reader) {
                Image image = null;
                try {
                    image = reader.acquireLatestImage();
                    ByteBuffer buffer = image.getPlanes()[0].getBuffer();
                    bytes = new byte[buffer.capacity()];
                    buffer.get(bytes);
                    save(bytes);
                } catch (Exception ee) {
                    Log.e("ImageReader Error ", ee.getMessage());
                } finally {
                    if (image != null)
                        image.close();
                }
            }

            void save(byte[] bytes) {

                File file12 = getOutputMediaFile();
                OutputStream outputStream = null;
                try {
                    assert file12 != null;
                    //file12 = new Compressor(VehicleEpaCamera.this).compressToFile(file12);
                    outputStream = new FileOutputStream(file12);
                    outputStream.write(bytes);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (outputStream != null)
                            outputStream.close();
                    } catch (Exception e) {
                    }
                }
            }
        };

        reader.setOnImageAvailableListener(imageAvailableListener, handler);
        previewSSession = new CameraCaptureSession.CaptureCallback() {
            @Override
            public void onCaptureStarted(CameraCaptureSession session, CaptureRequest request, long timestamp, long frameNumber) {
                super.onCaptureStarted(session, request, timestamp, frameNumber);
            }

            @Override
            public void onCaptureCompleted(CameraCaptureSession session, CaptureRequest request, TotalCaptureResult result) {
                super.onCaptureCompleted(session, request, result);

                try {
                    session.stopRepeating();
                } catch (CameraAccessException e) {
                    Toast.makeText(VehicleEpaCamera.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
                if (bytes != null) {

                    callCloudVision(bitmap, feature);

                } else {
                    Toast.makeText(VehicleEpaCamera.this, "Something went wrong", Toast.LENGTH_SHORT).show();
                    getPicture();
                }
            }
        };
        cameraDevice.createCaptureSession(outputSurfaces, new CameraCaptureSession.StateCallback() {
            @Override
            public void onConfigured(CameraCaptureSession session) {
                try {
                    session.capture(capturebuilder.build(), previewSSession, handler);
                } catch (Exception e) {
                }
            }

            @Override
            public void onConfigureFailed(CameraCaptureSession session) {
            }
        }, handler);
    } catch (Exception e) {
    }
}

0 个答案:

没有答案