简体   繁体   English

如何显示相机看到的片段?

[英]How to show what's camera seeing in fragment?

The application purpose is to basically compare what's camera seeing vs an image from the gallery. 该应用程序的目的是从根本上比较相机看到的图像与画廊图像。 I don't know which control gives you access to the camera without opening the default system app. 我不知道哪个控件可以在不打开默认系统应用程序的情况下访问相机。

I know this app will be useless on split-screen phones (camera on one side and gallery in the other) but is intended to be used in phones without this Nougat functionality (marshmallow or lollipop). 我知道这个应用程序在分屏手机(一侧为摄像头,另一侧为画廊)上将无用,但旨在用于没有这种牛轧糖功能(棉花糖或棒棒糖)的手机。

I've seen some apps that display camera like Barcode readers and some quick photo editors. 我看过一些显示相机的应用程序,例如条形码阅读器和一些快速的照片编辑器。

在此处输入图片说明

You can use Camera api. 您可以使用Camera api。 You will need to create your own SurfaceView to display preview of what camera is seeing. 您将需要创建自己的SurfaceView来显示所看到相机的预览。 There are a lot of tutorial on internet for this. 互联网上有很多关于此的教程。

public class ImageSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    private Camera camera;
    private SurfaceHolder surfaceHolder;

    public ImageSurfaceView(Context context, Camera camera) {
        super(context);
        this.camera = camera;
        this.surfaceHolder = getHolder();
        this.surfaceHolder.addCallback(this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            this.camera.setPreviewDisplay(holder);
            this.camera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

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

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        this.camera.stopPreview();
        this.camera.release();
    }
}



public class MainActivity extends Activity  implements SensorEventListener {
    private Camera mCamera;
    private ImageSurfaceView cameraView;

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

    protected void onResume(){
        super.onResume();
        mCamera = getCameraInstance();
        mCamera.setDisplayOrientation(90);
        cameraView = new ImageSurfaceView(this, mCamera);
        mainView.addView(cameraView);
        mainView.bringChildToFront(buttonView);
        senSensorManager.registerListener(this, senRotation, SensorManager.SENSOR_DELAY_GAME);
    }

    /** A safe way to get an instance of the Camera object. */
    public Camera getCameraInstance(){
        Camera c = null;
        try{
            c = Camera.open(); // attempt to get a Camera instance
            Camera.Parameters parameters = c.getParameters();
            parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
            c.setParameters(parameters);
        }
        catch (Exception e){
            // Camera is not available (in use or does not exist)
        }

        return c; // returns null if camera is unavailable
    }
}

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

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