Using android.view.SurfaceView with a camera on part of the screen

后端 未结 3 730
清酒与你
清酒与你 2020-12-08 23:54

I trying to put together an Android app that will take a picture and process it in some way. I\'d like the layout to be similar to Google Goggles. Meaning, camera preview

相关标签:
3条回答
  • 2020-12-09 00:27

    Yes you can use a surfaceView. From the surfaceView you can get the SurfaceHolder which then can be used to set the camera's previewDisplay.

            SurfaceView preview = (SurfaceView) findViewById(R.id.cameraPreview);
        SurfaceHolder previewHolder = preview.getHolder();
        previewHolder.addCallback(surfaceCallback);
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    

    You'll need to implement the SurfaceHolder.Callback method in order to update the preview size properly. In surfaceCreated set the previewDisplay of the camera. On surfaceChanged update the previewSize for the camera.

    camera.setPreviewDisplay(previewHolder);
    

    In fact the example mentioned before is a very good reference!

    0 讨论(0)
  • 2020-12-09 00:29

    If I am not missing something, you cannot just use SurfaceView as is. You need a derived class of your own. I usually create this view programmatically, and give it the full screen. But you can cover parts of it with other views, including buttons:

     setContentView(new CameraView());
     View mainscreen = getLayoutInflater().inflate(R.layout.mainscreen, null, false);
     ViewGroup.LayoutParams generalLayoutParam = new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.FILL_PARENT,
                        ViewGroup.LayoutParams.FILL_PARENT);
     addContentView(mainscreen, generalLayoutParam);
    
    0 讨论(0)
  • 2020-12-09 00:48

    First, your layout probably won't give you what you want. Consider using a RelativeLayout, with the Button anchored to the bottom, and the SurfaceView anchored to the top of the screen and the top of the Button.

    Also, you have a duplicate xmlns:android="http://schemas.android.com/apk/res/android" on your SurfaceView that you don't need. The one on your root element will suffice.

    In terms of your exception, there may be more detail in your stack trace that you are missing, explaining why startPreview() failed. Look for a "Caused by" or other line mid-way through the stack trace. If you can't identify it, edit your question and paste in the entire stack trace (and ping me via a comment on this answer, since I won't know about the edit otherwise).

    You might also experiment with this book example as another Camera preview app, to see if there's something about how I approach the problem that works better for your circumstance.

    0 讨论(0)
提交回复
热议问题