如何在表面android上预览镜像?

时间:2017-09-13 06:00:59

标签: android camera

我使用Surfaceview制作了相机预览应用。

但我的相机预览。如果我右手拿起,我的相机预览就会左手。

换句话说,我的相机预览应用翻转预览。

我希望像镜子一样预览。

我的来源

MainActivity.class

private final Context context;
private final FrameLayout liveFrame;
private CameraLiveView cameraLiveView;

public void onCreate() {
   liveFrame = (FrameLayout) activity.findViewById(R.id.liveViewLayout);
   try {
        LiveCamera liveCamera = CameraManage.getInstance();
        cameraLiveView = new CameraLiveView(this, liveCamera, this);

        if (liveFrame != null) {
            liveFrame.addView(cameraLiveView);
        }
   } catch (Exception e) {
        e.printStackTrace();
   } 
}

CameraLiveView.class

public class CameraLiveView extends SurfaceView implements SurfaceHolder.Callback {

static final String TAG = "CAMERA-LIVE-VIEW";

private SurfaceHolder mHolder;
private Camera mCamera;

private int previewWidth;
private int preViewHeight;
private int previewFormat;

private boolean playing = false;

public CameraLiveView(Context context, LiveCamera liveCamera) {
    super(context);

    mHolder = getHolder();
    mHolder.addCallback(this);

    mCamera = liveCamera.camera;

    previewWidth = liveCamera.previewSize.width;
    preViewHeight = liveCamera.previewSize.height;

    previewFormat = liveCamera.previewFormat;
}


@Override
public void surfaceCreated(SurfaceHolder holder) {

    cameraPlayStart();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    cameraPlayStop();
    }

public boolean cameraPlayStart() {

    if (mHolder == null)
        return false;

    try {
        Log.d(TAG, "camera play...;");
        mCamera.setPreviewDisplay(mHolder);
        mCamera.startFaceDetection();
        mCamera.startPreview();
        playing = true;
    } catch (IOException e) {
        Log.e(TAG, "camera play start fail: " + e.getMessage());
        return false;
    }

    return true;
}

public boolean cameraPlayStop() {
    boolean ret = false;

    if (playing == false) {
        return ret;
    }

    try {
        Log.d(TAG, "camera play stop...;");
        mCamera.stopSmoothZoom();
        mCamera.stopFaceDetection();
        mCamera.stopPreview();

        playing = false;

        ret = true;

    } catch (Exception e) {
        Log.d(TAG, "camera play stop fail: " + e.getMessage());
    }

    return ret;
  }


}

CameraManage.class

public class CameraManage {
static final String TAG = "Camera Manage";

public static final int PREVIEW_WIDTH = 640;
public static final int PREVIEW_HEIGHT = 480;

private static Camera mCamera = null;

public static LiveCamera getInstance() throws CameraInstanceException, IOException {

    if (mCamera == null) {
        mCamera = Camera.open(0);
        if (mCamera != null) {
            Log.d(TAG, "mCamera not null");
            mCamera.lock();
            drawAreas();
        }
    } else {

    }
    return getLiveCamera();
}

public static void returnInstance() {
    if (mCamera != null) {
        mCamera.unlock();
        mCamera = null;
    }
}


public static int getBufferSize(Camera.Parameters param) {

    Camera.Size liveViewSize = param.getPreviewSize();

    int bitsPerPixel = ImageFormat.getBitsPerPixel(param.getPreviewFormat());
    int bytePerPixel = bitsPerPixel / 8;

    int raw_image_bytesize =
            (liveViewSize.width * liveViewSize.height) * bytePerPixel;

    return raw_image_bytesize;
}

public static String formatNamed(int f) {
    String name = "";

    switch (f) {
        case ImageFormat.JPEG:
            name = "JPEG";
            break;
        case ImageFormat.NV16:
            name = "NV16";
            break;
        case ImageFormat.NV21:
            name = "NV16";
            break;
        case ImageFormat.RAW10:
            name = "RAW10";
            break;
        case ImageFormat.RAW_SENSOR:
            name = "RAW_SENSOR";
            break;
        case ImageFormat.RGB_565:
            name = "RGB_565";
            break;
        case ImageFormat.YUV_420_888:
            name = "YUV_420_888";
            break;
        case ImageFormat.YUY2:
            name = "YUY2";
            break;
        case ImageFormat.YV12:
            name = "YV12";
            break;
        case ImageFormat.UNKNOWN:
        default:
            name = "unknown";
    }

    return name;
}


private static void drawAreas() {
    Camera.Parameters params = mCamera.getParameters();
    try {
        params.setPreviewSize(PREVIEW_WIDTH, PREVIEW_HEIGHT);

        List<Integer> previewFormats = params.getSupportedPreviewFormats();
        if (previewFormats.contains(ImageFormat.RGB_565)) {
            params.setPreviewFormat(ImageFormat.RGB_565);
            Log.d(TAG, "set Preview -> RGB_565");
        }
        else if (previewFormats.contains(ImageFormat.YUY2)) {
            params.setPreviewFormat(ImageFormat.YUY2);
            Log.d(TAG, "set Preview -> YUY2");
        }
        else if (previewFormats.contains(ImageFormat.YV12)) {
            params.setPreviewFormat(ImageFormat.YV12);
            Log.d(TAG, "set Preview -> YV12");
        }

        mCamera.setParameters(params);
    } catch (RuntimeException e) {
    } finally {
        params = mCamera.getParameters();
        Camera.Size newSize = params.getPreviewSize();
        Log.d(TAG, "preview format: " + formatNamed(params.getPreviewFormat()));

    }
}

public static void setCameraDisplayOrientation(Activity activity,
                                               int cameraId, Camera camera) {
    Camera.CameraInfo info =
            new Camera.CameraInfo();
    Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();
    int degrees = 0;
    Log.d(TAG, "rotation ->" + rotation);
    switch (rotation) {
        case Surface.ROTATION_0:
            degrees = 0;
            break;
        case Surface.ROTATION_90:
            degrees = 90;
            break;
        case Surface.ROTATION_180:
            degrees = 180;
            break;
        case Surface.ROTATION_270:
            degrees = 270;
            break;
    }


    int result;
    result = (info.orientation + degrees) % 360;
    result = (360 - result) % 360;  // compensate the mirror

    camera.setDisplayOrientation(result);
}


/**
 *
 * @return
 */
static private LiveCamera getLiveCamera() {
    LiveCamera liveCamera = new LiveCamera();

    Camera.Parameters params = mCamera.getParameters();
    Camera.Size previewSize = params.getPreviewSize();

    liveCamera.previewSize = params.getPreviewSize();
    liveCamera.pictureSize = params.getPictureSize();
    liveCamera.previewFormat = params.getPreviewFormat();
    liveCamera.buffer_size = getBufferSize(params);
    liveCamera.camera = mCamera;


    return liveCamera;
 }

}

LiveCamera.class

public class LiveCamera {
   public Camera camera;
   public Camera.Size previewSize;
   public Camera.Size pictureSize;
   public int previewFormat;
   public int buffer_size;
   public Capturable capturable;
}

这个源只有相机翻转预览。 如何在android上镜像模式预览?

感谢。

0 个答案:

没有答案