简体   繁体   中英

Android Camera preview issue with FrameLayout

在此处输入图片说明 This is my XML file. I take one LinearLayout in which i take SurfaceView and Button, but camera displays view like following Image... look at Image and please give me solution.

 <LinearLayout 
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <SurfaceView 
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/surface"
        android:layout_weight="2"/>
     <Button 
        android:layout_height="100dp"
        android:layout_width="match_parent"
        android:id="@+id/click"
        android:text="Click Photo"
        android:layout_weight="1"/>
</LinearLayout>  

and here is my java code please check this also

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    clickphotobtn=(Button)findViewById(R.id.click);
    surfaceview=(SurfaceView)findViewById(R.id.surface);
    surfaceholder=surfaceview.getHolder();
    surfaceholder.addCallback(this);
    surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    clickphotobtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            //startActivity(intent);
            camera.takePicture(null, null, null);

        }
    });

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
     try {
         camera = Camera.open();  
         camera.setPreviewDisplay(surfaceholder);
     // Toast.makeText(getApplication(), "Create", Toast.LENGTH_LONG).show();
     } catch (IOException e) { }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
        int height) 
{
     Camera.Parameters parameters = camera.getParameters();
     Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

     if(display.getRotation()== Surface.ROTATION_0)
     {
         parameters.setPreviewSize(width, height);
         camera.setDisplayOrientation(90);
     }
     if(display.getRotation()== Surface.ROTATION_90 || display.getRotation()== Surface.ROTATION_180)
         parameters.setPreviewSize(width, height);
     if(display.getRotation()==Surface.ROTATION_270)
     {
         parameters.setPreviewSize(width, height);
         camera.setDisplayOrientation(180);
     }
    //   camera.setParameters(parameters);
     try{
         camera.setPreviewDisplay(surfaceholder);
         camera.startPreview();          
     }
     catch (Exception e){
         Toast.makeText(getApplication(), e.toString(), Toast.LENGTH_LONG).show();
     }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (null == camera)
        return;
    camera.stopPreview();
    camera.release();
    camera = null;
    preview = false;
    //Toast.makeText(getApplication(), "Destroy", Toast.LENGTH_LONG).show();
}

在此处输入图片说明

This is the right way to change camera orientation, this piece of code worked for me. camera.setDisplayOrientation(90);

Try to follow the way suggested by the documentation, using Camera.CameraInfo in order to obtain the camera orientation and updating it consequently.
Look at the example here: void setDisplayOrientation(int) .

添加公共类YourActivity扩展AppCompatActivity实现SurfaceHolder.Callback {....}

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