简体   繁体   English

尝试在SurfaceView中进行设置时拉伸了相机预览

[英]Camera preview stretched when trying to set in SurfaceView

I am trying to set Camera preview in SurfaceView When I set Camera in Surface it looks like stretched preview. 我试图在SurfaceView中设置相机预览当我在Surface中设置相机时,它看起来像拉伸的预览。 How can I solve this? 我该如何解决? 在此处输入图片说明

`public class CamActivity extends Activity implements SurfaceHolder.Callback`
`{`
`Camera camera;`
`SurfaceView surface;`
`SurfaceHolder mholder;`
`Button capture;`
`Bitmap bitmap;`
`public  String  path = Environment.getDataDirectory().getAbsolutePath() + "/storage/emulated/0/Pictures/Cam";`
@Override
`protected void onCreate(Bundle savedInstanceState) `
`{`
 `   super.onCreate(savedInstanceState);`
  `  setContentView(R.layout.activity_cam);`
   ` surface=(SurfaceView)findViewById(R.id.camera_view);`
   ` if(mholder==null)`
    `   mholder=surface.getHolder();`
   ` mholder.addCallback(this);`
    `mholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);`

    `capture=(Button)findViewById(R.id.camera_capture);`
    `File mFolder = new File(path);`
    `if (!mFolder.exists()) {`
     `   mFolder.mkdir();`
    `}`
    `capture.setOnClickListener(new OnClickListener() {`

    `   @SuppressWarnings("deprecation")`
    `   @Override`
    `   public void onClick(View v) {`
    `        camera.takePicture(null, null, new PictureCallback()` 
    `        {`

                @Override
    `           public void onPictureTaken(byte[] data, Camera camera)`
    `           {`

    `               Random generator = new Random();`
    `               int n = 10000;`
    `               n = generator.nextInt(n);`
    `               String fname = "Image-"+ n +".jpg";`
    `               File pictureFile = new File(Environment.getExternalStoragePublicDirectory(`
    `                         Environment.DIRECTORY_PICTURES)+"/", fname);`
    `                try {`
    `                       FileOutputStream fos = new FileOutputStream(pictureFile);`
    `                       bitmap.compress(Bitmap.CompressFormat.JPEG,90, fos);`
    `                       fos.flush();`
    `                       fos.close();`
    `                   } catch (FileNotFoundException e) {`
                            `Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();`
    `                   } catch (IOException e) {`
                            `Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();`
    `                   }`

    `           }`
    `       });`
    `   }`
    `});`
`}`

@Override
`public void surfaceCreated(SurfaceHolder holder) `
`{`
`    camera=Camera.open();`
`    try` 
`    {`
`        camera.setPreviewDisplay(holder);`
`        Toast.makeText(getApplicationContext(), path, Toast.LENGTH_LONG).show();`
`    } `
`    catch (IOException exception)` 
`    {`
`         camera.release();`
`         camera = null;`
`    }`
`}`

@Override
`public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) `
`{`
 `   camera.startPreview();`
  `  camera.setDisplayOrientation(90);`
`}`

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

You need to listen to orientation change in the activity and set the proper orientation to the camera. 您需要聆听活动中的方向变化,并为相机设置正确的方向。

Add this method to your camera activity: 将此方法添加到相机活动中:

public void setCameraDisplayOrientation(Activity activity) {

    if(null == mCamera){
        return;
     }

       android.hardware.Camera.CameraInfo info = 
           new android.hardware.Camera.CameraInfo();

       android.hardware.Camera.getCameraInfo(cameraId, info);

       int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
       int degrees = 0;

       switch (rotation) {
           case Surface.ROTATION_0: degrees = 0; break;
           case Surface.ROTATION_90: degrees = 90; break;
           case Surface.ROTATION_180: degrees = 180; break;
           case Surface.ROTATION_270: degrees = 270; break;
       }


       if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
           orientation = (info.orientation + degrees) % 360;
           orientation = (360 - orientation) % 360;  // compensate the mirror
       } else {  // back-facing
           orientation = (info.orientation - degrees + 360) % 360;
       }

       if(null != mCamera){
           mCamera.setDisplayOrientation(orientation);
       }
    }

also add OrientationEventListner 还添加OrientationEventListner

    mOrientationEventListener = new OrientationEventListener(mApplication, 
            SensorManager.SENSOR_DELAY_NORMAL) {

        @Override
        public void onOrientationChanged(int orientation) {

            if ((orientation == ORIENTATION_UNKNOWN) || (mCamera == null)) {
                return;
            }

            Camera.Parameters params                = mCamera.getParameters();               
            android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();

            android.hardware.Camera.getCameraInfo(cameraId, info);

            orientation = (orientation + 45) / 90 * 90;

            int rotation = 0;

            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                rotation = (info.orientation - orientation + 360) % 360;
            }
            else {  
                /*
                 * back-facing camera
                 */
                rotation = (info.orientation + orientation) % 360;
            }

            params.setRotation(rotation);

            if(null == mCamera) {
                return;
            }

            mCamera.setParameters(params);
        }

    };

Enable orientation listener once the activity starts /* * start orientation listener */ 活动开始后启用方向监听器/ * *启动方向监听器* /
if(mOrientationEventListener.canDetectOrientation()){ 如果(mOrientationEventListener.canDetectOrientation()){
mOrientationEventListener.enable(); mOrientationEventListener.enable(); } }

and in the onConfigurationChanged and onResume callback of the activity , make the following call 在活动的onConfigurationChanged和onResume回调中,进行以下调用

setCameraDisplayOrientation(Activity activity) setCameraDisplayOrientation(活动活动)

Hope this helps 希望这可以帮助

Regards, Shrish 问候,Shrish

EDIT UPDATE: Please check out this sample code for camera , most of your doubts should get cleared https://github.com/shrishmv/CameraTest 编辑更新:请查看此照相机的示例代码,您的大部分疑虑都应清除https://github.com/shrishmv/CameraTest

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

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