如何在android上使用矩阵相机预览翻页?

时间:2016-12-14 04:16:39

标签: android

我希望相机预览屏幕左右反转

首先,我的设备不使用Camera.CameraInfo.CAMERA_FACING_FRONT

因为,不支持FRONT CAMERA,仅支持CAMERA_FACING_BACKCamera.open(0)

并查看我的MainActivity.class

public class MainActivity extends Activity implements TextureView.SurfaceTextureListener {
    private Camera mCamera;
    private TextureView mTextureView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.
        mTextureView = (TextureView) findViewById(R.id.camera_texture);
        mTextureView.setSurfaceTextureListener(this);
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
    int cameraId = 0;
    Camera.CameraInfo info = new Camera.CameraInfo();
    for (cameraId = 0; cameraId < Camera.getNumberOfCameras(); cameraId ++ ) {
        Camera.getCameraInfo(0, info);

        if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) 
           break;
        }
    mCamera = Camera.open(cameraId);
    Matrix transform = new Matrix();

    Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
    int rotation = getWindowManager().getDefaultDisplay().getRotaion();            
    switch (rotation) {
        case Surface.ROTATION_0:
            mCamera.setDisplayOrientation(90);
            mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
                    previewSize.height, previewSize.width, Gravity.CENTER));
            transform.setScale(-1, 1, previewSize.height/2, 0);
            break;

        case Surface.ROTATION_90:
            mCamera.setDisplayOrientation(0);
            mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
                    previewSize.width, previewSize.height, Gravity.CENTER));
            transform.setScale(-1, 1, previewSize.width/2, 0);
            break;

        case Surface.ROTATION_180:
            mCamera.setDisplayOrientation(270);
            mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
                    previewSize.height, previewSize.width, Gravity.CENTER));
            transform.setScale(-1, 1, previewSize.height/2, 0);
            break;

        case Surface.ROTATION_270:
            mCamera.setDisplayOrientation(180);
            mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
                    previewSize.width, previewSize.height, Gravity.CENTER));
            transform.setScale(-1, 1, previewSize.width/2, 0);
            break;
    }


    try {
        mCamera.setPreviewTexture(surfaceTexture);
    } catch (IOException t) {
    }

    mTextureView.setTransform(transform);
    Log.i("onSurfaceTextureAvailable", "Transform: " + transform.toString());

    mCamera.startPreview();

}

这个来源只是旋转, 所以不要左右反向预览。

我想要翻转相机预览。

如何在Android使用矩阵上翻转相机预览?

0 个答案:

没有答案