How to capture image from custom CameraView in Android?

后端 未结 6 697
孤街浪徒
孤街浪徒 2020-11-29 18:36

i need to capture image from required portion of the screen.

capture image from camera.

6条回答
  •  我在风中等你
    2020-11-29 19:14

    I use the CameraPreview in ApiDemos application and edit it as your requirement.

    First, copy the code of the Preview class into a new class file in the same package so that it is public and you can declare it in the xml layout file. Remember to add one more constructor as below:

    public Preview(Context context, AttributeSet attrs) {
        super(context, attrs);
        mSurfaceView = new SurfaceView(context);
        addView(mSurfaceView);
    
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }
    

    Sample layout file with sample width and height:

    
    
    
        
    
       
            
    
       
    
    
    

    In the onCreate() method of the CameraPreview activity, change the setContentView part as followed:

    setContentView(R.layout.camera_layout);
    mPreview = (Preview) findViewById(R.id.camera_view);
    

提交回复
热议问题