简体   繁体   English

在Android中初始化相机后,如何反转图像?

[英]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. 不同的设备可能具有不同的传感器方向,因此您应根据Devise的方向和传感器的方向自己计算最终方向。 To get sensor orientation use 要获取传感器方向,请使用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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