android - 自定义相机 textureView 未调整为横向模式

标签 android orientation android-camera2

我正在制作一个自定义的安卓相机应用。到目前为止,我遇到的唯一问题是让 textureView 在横向模式下进行调整(将相机旋转 90 度)。 View 出来扭曲,而不是在正确的方向。肖像模式工作正常。我不确定问题出在 setupCamera() 方法、sensorToDeviceRotation() 方法还是 configureTransform() 方法中。我还没有实现任何实际拍摄照片的措施。感谢您的帮助。

编辑/更新:我更新了我的代码以添加改进。尽管如此,原来的问题仍然存在。我正在使用 Pixel XL 来测试这段代码。我仍然不确定问题出在哪里。

主页.java

public class homePage extends AppCompatActivity {

    private static final int CAMERA_PERMISSION = 123;
    private String mCameraId;
    private Size mPreviewSize;
    private CaptureRequest.Builder mCaptureRequestBuilder;
    private HandlerThread mBackgroundHandlerThread;
    private Handler mBackgroundHandler;
    private static SparseIntArray ORIENTATIONS = new SparseIntArray();
    static {
        ORIENTATIONS.append(Surface.ROTATION_0, 0);
        ORIENTATIONS.append(Surface.ROTATION_90, 90);
        ORIENTATIONS.append(Surface.ROTATION_180, 180);
        ORIENTATIONS.append(Surface.ROTATION_270, 270);
    }

    private CameraDevice mCameraDevice;
    private CameraDevice.StateCallback mCameraDeviceCallback = new CameraDevice.StateCallback() {
        @Override
        public void onOpened(CameraDevice camera) {
            mCameraDevice = camera;
            //Toast.makeText(getApplicationContext(), "Camera connected", Toast.LENGTH_SHORT).show();
            startPreview();
        }
        @Override
        public void onDisconnected(CameraDevice camera) {
            camera.close();
            mCameraDevice = null;
        }
        @Override
        public void onError(CameraDevice camera, int error) {
            camera.close();
            mCameraDevice = null;
        }
    };

    private TextureView mTextureView;
    private TextureView.SurfaceTextureListener mSurfaceTextureListener = new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            setupCamera(width, height);
            connectCamera();

        }
        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            configureTransform(width, height);
        }
        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            return false;
        }
        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {

        }
    };

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String permissions[],
                                           @NonNull int[] grantResults) {

        switch(requestCode) {
            case CAMERA_PERMISSION:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    return;
                }else {
                    Toast.makeText(this, "Certain permissions needed to continue", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    private boolean hasCamera() {

        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)) {
            return true;
        }else {

            AlertDialog camAlert = new AlertDialog.Builder(homePage.this).create();
            camAlert.setTitle("Alert");
            camAlert.setMessage("there is no usable camera");
            camAlert.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            camAlert.show();
            return false;
        }
    }

    private void setupCamera(int width, int height) {
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        try {
            for (String cameraId : cameraManager.getCameraIdList()) {
                CameraCharacteristics cameraCharacteristics = cameraManager.getCameraCharacteristics(cameraId);

                if(cameraCharacteristics.get(CameraCharacteristics.LENS_FACING) ==
                        CameraCharacteristics.LENS_FACING_FRONT) {
                    continue;
                }
                StreamConfigurationMap map = cameraCharacteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
                int deviceOrientation = getWindowManager().getDefaultDisplay().getRotation();
                int totalRotation = sensorToDeviceRotation(cameraCharacteristics, deviceOrientation);
                boolean swapRotation = totalRotation == 90 || totalRotation == 270;
                int rotatedWidth = width;
                int rotatedHeight = height;

                if(swapRotation) {
                    rotatedWidth = height;
                    rotatedHeight = width;
                }

                mPreviewSize = chooseOptimalSize(map.getOutputSizes(SurfaceTexture.class), rotatedWidth, rotatedHeight);
                mCameraId = cameraId;
                return;
            }
        } catch (CameraAccessException e){
        e.printStackTrace();
        }
    }

    private void closeCamera() {
        if(mCameraDevice != null) {
            mCameraDevice.close();
            mCameraDevice = null;
        }
    }

    private void connectCamera() {

        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) ==
                        PackageManager.PERMISSION_GRANTED) {
                    cameraManager.openCamera(mCameraId, mCameraDeviceCallback, mBackgroundHandler);
                    //Toast.makeText(this, "Camera permission granted", Toast.LENGTH_SHORT).show();
            } else {
                    if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
                        //Toast.makeText(this, "non-marshmellow device permission granted", Toast.LENGTH_SHORT).show();
                    }
                    requestPermissions(new String[] {Manifest.permission.CAMERA}, CAMERA_PERMISSION);
                }
            } else {
                cameraManager.openCamera(mCameraId, mCameraDeviceCallback, mBackgroundHandler);
            }

        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    private void startPreview() {
        SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
        surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
        Surface previewSurface = new Surface(surfaceTexture);

        try {
            mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
            mCaptureRequestBuilder.addTarget(previewSurface);

            mCameraDevice.createCaptureSession(Arrays.asList(previewSurface),
                    new CameraCaptureSession.StateCallback() {
                        @Override
                        public void onConfigured(CameraCaptureSession session) {
                            try {
                                session.setRepeatingRequest(mCaptureRequestBuilder.build(), null, mBackgroundHandler);
                            } catch (CameraAccessException e) {
                                e.printStackTrace();
                            }
                        }
                        @Override
                        public void onConfigureFailed(CameraCaptureSession session) {
                            Toast.makeText(getApplicationContext(), "Unable to set up camera preview", Toast.LENGTH_SHORT).show();

                        }
                    }, null);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

    private void startBackgroundThread() {
        mBackgroundHandlerThread = new HandlerThread("cameraString");
        mBackgroundHandlerThread.start();
        mBackgroundHandler = new Handler(mBackgroundHandlerThread.getLooper());
    }

    private void stopBackgroundThread() {
        mBackgroundHandlerThread.quitSafely();

        try {
            mBackgroundHandlerThread.join();
            mBackgroundHandlerThread = null;
            mBackgroundHandler = null;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static int sensorToDeviceRotation(CameraCharacteristics cameraCharacteristics,
                                              int deviceOrientation) {

        int sensorOrientation = cameraCharacteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
        deviceOrientation = ORIENTATIONS.get(deviceOrientation);
        return (sensorOrientation + deviceOrientation + 360) % 360;
    }

    private void configureTransform (int width, int height) {

        if (null == mTextureView || null == mPreviewSize) {
            return;
        }
        final int rotation = getWindowManager().getDefaultDisplay().getRotation();
        final Matrix matrix = new Matrix();
        final RectF viewRect = new RectF(0, 0, width, height);
        final RectF buffetRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
        final float centerX = viewRect.centerX();
        final float centerY = viewRect.centerY();

        if(Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
            buffetRect.offset(centerX - buffetRect.centerX(), centerY - buffetRect.centerY());
            matrix.setRectToRect(viewRect, buffetRect, Matrix.ScaleToFit.FILL);
            final float scale = Math.max(
                    (float) height / mPreviewSize.getHeight(),
                    (float) width / mPreviewSize.getWidth());
            matrix.postScale(scale, scale, centerX, centerY);

        }else if (Surface.ROTATION_180 == rotation) {
            matrix.postRotate(180, centerX, centerY);
        }
        mTextureView.setTransform(matrix);
    }
//=====added code from suggestions=======================
    public interface CameraModule {
        void onOrientationChanged(int orientation);
    }

    CameraModule mCurrentModule;
    private MyOrientationEventListener mOrientationListener;
    private int mLastRawOrientation = OrientationEventListener.ORIENTATION_UNKNOWN;

    private class MyOrientationEventListener extends OrientationEventListener {
        public MyOrientationEventListener(Context context){
            super(context);
        }

        @Override
        public void onOrientationChanged(int orientation) {
            if(orientation == ORIENTATION_UNKNOWN)
                return;
            mLastRawOrientation = orientation;
            mCurrentModule.onOrientationChanged(orientation);
        }
    }
//========================================================================
    private static class CompareSizeByArea implements Comparator<Size> {

        @Override
        public int compare(Size lhs, Size rhs) {
            return Long.signum((long) lhs.getWidth() * lhs.getHeight() /
                    (long) rhs.getWidth() * rhs.getHeight());
        }
    }

    private static Size chooseOptimalSize(Size[] choices, int width, int height) {
        List<Size> bigEnough = new ArrayList<Size>();

        for(Size option: choices) {
            if (option.getHeight() == option.getWidth() * height / width &&
                    option.getWidth() >= width && option.getHeight() >= height) {
                bigEnough.add(option);
            }
        }
        if (bigEnough.size() > 0) {
            return Collections.min(bigEnough, new CompareSizeByArea());
        } else {
            return choices[0];
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        hasCamera();
        mTextureView = (TextureView) findViewById(R.id.textureView);

        mOrientationListener = new MyOrientationEventListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        startBackgroundThread();

        if (mTextureView.isAvailable()) {
            setupCamera(mTextureView.getWidth(), mTextureView.getHeight());
            connectCamera();
        } else {
            mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
        }
    }

    @Override
    protected void onPause() {
        closeCamera();
        stopBackgroundThread();
        super.onPause();
    }
}

最佳答案

你可以尝试这样的事情(在你的 HomePage.java 中添加这个 private 类):注意 onOrientationChanged() 方法可以是也应用于 xmls: 我们保留最后已知的方向。因此,如果您首先调整相机的方向,然后将相机指向地板或天空,那么 TextureViewCamera 也会有正确的方向。

private MyOrientationEventListener mOrientationListener;
// The degrees of the device rotated clockwise from its natural orientation.
private int mLastRawOrientation= OrientationEventListener.ORIENTATION_UNKNOWN;
//Don't forget to add **mOrientationListener = new MyOrientationEventListener(this);** on your onCreate() method

private class MyOrientationEventListener
        extends OrientationEventListener {
    public MyOrientationEventListener(Context context) {
        super(context);
    }


 @Override
    public void onOrientationChanged(int orientation) {
        if (orientation == ORIENTATION_UNKNOWN) return;
        mLastRawOrientation = orientation;
        mCurrentModule.onOrientationChanged(orientation);
    }
}

有关更多信息,您可以点击此链接:Camera Example

关于android - 自定义相机 textureView 未调整为横向模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45775548/

相关文章:

android - 如何从 Activity 和 BroadcastReceiver 重现应用程序无响应 (ANR)

android - Azure BLOB 存储 REST API - 使用 ADAL 访问 token 返回 403 和 404 错误

android - 旋转后的TextView : are these problems bugs?

android - Mono for Android - 使用 Visual Studio 进行单元测试

iphone - 手机网站 : Orientation change from portrait to horizontal

android - 如何在 Camera2 API Android 中捕获叠加层内的图像?

java - Android Camera2 API,限制焦距的最佳方法?

android - 变量切换方向丢失

iOS 7 - SplitViewController 详细 View - 自动布局 UITextView 键盘方向更改

android - 这是 Android 中用于图像处理的最佳 Camera API