当前位置:  开发笔记 > 编程语言 > 正文

在Landscape中,Android Camera2 Preview旋转90度

如何解决《在Landscape中,AndroidCamera2Preview旋转90度》经验,为你挑选了1个好方法。

我正在关注youtube教程,试图学习Camera2 API.当然,我正在学习这个,同时我正在开发自己的应用程序.教程和我的应用程序之间的一个不一致之处在于,只有当我的应用程序必须处于横向状态时,教程才能使相机处于纵向模式.

我目前能够查看相机的预览,但是当我的应用程序是横向或水平时,相机预览看起来旋转了90度.这几乎感觉我可以旋转TextureView,但这看起来似乎不正确,就像我拍照时一样,它会被错误地旋转.

下面是与图像大小有关的代码(整个代码很长)

private void setupCamera(int width, int height) {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(camera_id);
        StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
        mPreviewSize = getPreferredPreviewSize(map.getOutputSizes(SurfaceTexture.class), width, height);
        mCameraId = camera_id;
    } catch (CameraAccessException e) {
        e.printStackTrace();
    }
}


//TODO Look for a way to make this horizontal
private Size getPreferredPreviewSize(Size[] mapSizes, int width, int height) {
    List collectorSizes = new ArrayList<>();
    for (Size option : mapSizes) {
        if (width > height) { //If the screen is in landscape
            Toast.makeText(getApplicationContext(), "Screen is Landscape", Toast.LENGTH_SHORT).show();
            if (option.getWidth() > width && option.getHeight() > height) {
                collectorSizes.add(option);
            }
        } else { //if the screen is in portrait
            Toast.makeText(getApplicationContext(), "Screen is Portrait", Toast.LENGTH_SHORT).show();
            if (option.getWidth() > height && option.getHeight() > width) {
                collectorSizes.add(option);
            }
        }
    }
    if (collectorSizes.size() > 0) {
        return Collections.min(collectorSizes, new Comparator() {
            @Override
            public int compare(Size lhs, Size rhs) {
                return Long.signum(lhs.getWidth() * lhs.getHeight() - rhs.getWidth() + rhs.getHeight());
            }
        });
    }

    return mapSizes[0];
}
private void openCamera() {
    CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    try {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        cameraManager.openCamera(mCameraId, mCameraDeviceStateCallback, null);
    } catch (CameraAccessException e){
        e.printStackTrace();
    }
}

private void createCameraPreviewSession() {
    try {
        SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
        surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        Surface previewSurface = new Surface(surfaceTexture);
        mPreviewCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
        mPreviewCaptureRequestBuilder.addTarget(previewSurface);
        mCameraDevice.createCaptureSession(Arrays.asList(previewSurface),
                new CameraCaptureSession.StateCallback() {
                    @Override
                    public void onConfigured(CameraCaptureSession session) {
                        if(mCameraDevice == null){
                            return;
                        }
                        try {
                            mPreviewCaptureRequest = mPreviewCaptureRequestBuilder.build();
                            mCameraCaptureSession = session;
                            mCameraCaptureSession.setRepeatingRequest(mPreviewCaptureRequest, mSessionCaptureCallback, null);
                        } catch (CameraAccessException e){
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onConfigureFailed(CameraCaptureSession session) {
                        Toast.makeText(getApplicationContext(), "Preview Session Failed", Toast.LENGTH_SHORT).show();
                    }
                }, null);
    } catch (CameraAccessException e){
        e.printStackTrace();
    }
}

我一直在玩这种getPreferredPreviewSize方法,但是我不理解它.我不确定compare最后使用lhsrhs.

我错过了一些简单的旋转方法吗?



1> 小智..:
private void transformImage (int width, int height)
{
    if(mPreviewSize == null || mTextureView == null)
    {
        return;
    }
    Matrix matrix = new Matrix();
    int rotation = getWindowManager().getDefaultDisplay().getRotation();
    RectF textureRectF = new RectF(0,0,width,height);
    RectF previewRectF = new RectF(0,0,mPreviewSize.getHeight(),mPreviewSize.getWidth());
    float centerX = textureRectF.centerX();
    float centery = textureRectF.centerY();

    if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_270)
    {}
    else if(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270)
    {
        previewRectF.offset(centerX - previewRectF.centerX(),centery-previewRectF.centerY());
        matrix.setRectToRect(textureRectF,previewRectF,Matrix.ScaleToFit.FILL);
        float scale = Math.max((float)width / mPreviewSize.getWidth(),(float)height/ mPreviewSize.getHeight());

        matrix.postScale(scale,scale,centerX,centery);
        matrix.postRotate(90*(rotation-2),centerX,centery);
        mTextureView.setTransform(matrix );

    }

推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有