尝试在SurfaceView中设置时,相机预览会拉长

时间:2014-11-22 08:46:45

标签: android android-layout android-camera surfaceview screen-resolution

我正在尝试在SurfaceView中设置相机预览 当我在Surface中设置Camera时,它看起来像是拉伸预览。 我该如何解决这个问题?enter image description here

`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;`        
`}`
`}`

1 个答案:

答案 0 :(得分:2)

您需要在活动中聆听方向更改并为相机设置正确的方向。

将此方法添加到相机活动中:

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);
       }
    }

还添加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);
        }

    };

活动开始后启用方向侦听器         / *          *开始定向听众          * /
        如果(mOrientationEventListener.canDetectOrientation()){
            mOrientationEventListener.enable();         }

并在活动的onConfigurationChanged和onResume回调中,进行以下调用

setCameraDisplayOrientation(活动活动)

希望这有帮助

此致 Shrish

编辑更新: 请查看相机的示例代码,大部分疑问都应该清除 https://github.com/shrishmv/CameraTest