简体   繁体   中英

How to invert the image when the camera is initialized in Android?

When the camera is initialized, if the device is vertically, image upside down and backwards on the front, and if device horizontally, image upside down. How to make the right conclusion pictures?

**Code hire **

package com.example.aleksey.camera;

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;

public class CameraPreview extends SurfaceView implements            SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;

public CameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
mHolder = getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {

try {
    // create the surface and start camera preview
    if (mCamera == null) {
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
    }
} catch (Exception e) {
    Toast.makeText(getContext(), "Error setting camera preview",     Toast.LENGTH_SHORT).show();
}
}

public void surfaceChanged(SurfaceHolder holder, int format, int w,  int h) {
refreshCamera(mCamera);
}

public void refreshCamera(Camera camera) {
if (mHolder.getSurface() == null) {
    return;
}
try {
    mCamera.stopPreview();
} catch (Exception e) {
}

mCamera = camera;
try {
    mCamera.setPreviewDisplay(mHolder);
    mCamera.startPreview();
} catch (Exception e) {
    Toast.makeText(getContext(), "Error starting camera preview:",  Toast.LENGTH_SHORT).show();
}
}

@Override`enter code here
public void surfaceDestroyed(SurfaceHolder holder) {
 mCamera.release();
}

//     camera.setDisplayOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

At the end of the code have uncommented function like it should work, but I've tried all the options and without success.

Try to set

camera.setDisplayOrientation(90);

With correct orientation. Different devices may have different sensor orientations, so you may should calculate the final orientation yourself, depending on Devise's orientation and sensor's orientation. To get sensor orientation use

 Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
 Camera.getCameraInfo(cameraIndex, cameraInfo);
 int sensorOrientation = cameraInfo.orientation;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM