使用Android自定义相机扭曲图像

时间:2013-08-31 07:27:55

标签: android android-camera

我正在Android中构建自己的自定义相机。当我打开相机然后它的工作完美,但在横向模式下,图像显示失真。

任何人都可以帮我解决。我的自定义相机代码是: -

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {

private SurfaceHolder mSurfaceHolder;
private Camera mCamera;

//Constructor that obtains context and camera
public CameraPreview(Context context, Camera camera) {
    super(context);
    this.mCamera = camera;

    this.mSurfaceHolder = this.getHolder();
    this.mSurfaceHolder.addCallback(this); // we get notified when underlying surface is created and destroyed
    this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //this is a deprecated method, is not requierd after 3.0
}

@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
    try {
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (IOException e) {
      // left blank for now
    }

}

@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
    mCamera.stopPreview();
    mCamera.release();
}

@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
        int width, int height) {






    // start preview with new settings
    try {



        DVCameraActivity a=new DVCameraActivity();




        Camera.Parameters parameters = mCamera.getParameters();

         parameters.set("zoom", "2.0");
           parameters.set("taking-picture-zoom", "20");

         Camera.Size size = getBestPreviewSize(width, height) ;

         parameters.setPreviewSize(size.width, size.height);


            int displayRotation = a.display.getRotation();


          switch (displayRotation)  {
                case Surface.ROTATION_0:     
                    mCamera.setDisplayOrientation(90);    

                    //Log.w("enter here","aaa");break;

                case Surface.ROTATION_90:    
                     parameters.setPreviewSize(size.width, size.height);
                    break;

                case Surface.ROTATION_180: 
                     parameters.setPreviewSize(size.height,size.width);

                    break;

                case Surface.ROTATION_270:    
                 parameters.setPreviewSize(size.width, size.height);

                    mCamera.setDisplayOrientation(180);

                //  Log.w("enter here","aaa");
                break;

            }





        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    } catch (Exception e) {
        // intentionally left blank for a test
    }
}


private Camera.Size getBestPreviewSize(int width, int height)
{
        Camera.Size result=null;    
        Camera.Parameters p = mCamera.getParameters();
        for (Camera.Size size : p.getSupportedPreviewSizes()) {
            if (size.width<=width && size.height<=height || (size.height <= width && size.width <= height)) {
                if (result==null) {
                    result=size;
                } else {
                    int resultArea=result.width*result.height;
                    int newArea=size.width*size.height;

                    if (newArea>resultArea) {
                        result=size;
                    }
                }
            }
        }
    return result;

}

}

我的主要活动是: -

 public class DVCameraActivity extends Activity {
    private Camera mCamera;
    private CameraPreview mCameraPreview;
    public static final int MEDIA_TYPE_IMAGE = 1;

  static  Display display;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dvcamera);

 //    display  = getWindowManager().getDefaultDisplay();

        display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

        mCamera = getCameraInstance();


        mCameraPreview = new CameraPreview(this, mCamera);



        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mCameraPreview);


      //Adding listener
        Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        mCamera.takePicture(null, null, mPicture);

                    }
                });

    }

    /**
     * Helper method to access the camera returns null if
     * it cannot get the camera or does not exist
     * @return
     */
    private Camera getCameraInstance() {
        Camera camera = null;

        try {
            camera = Camera.open();
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }


    PictureCallback mPicture = new PictureCallback() {

        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
             File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                if (pictureFile == null){
                    return;
                }

                try {
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    fos.write(data);
                    fos.close();
                } catch (FileNotFoundException e) {

                } catch (IOException e) {

                }

        }

    };



    /** Create a File for saving the image */
    private static File getOutputMediaFile(int type){

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES), "MyCameraApp");

        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
            "IMG_"+ timeStamp + ".jpg");

        return mediaFile;
    }


    @Override
    protected void onStop() {
    super.onStop();

   // mCamera.release();
    }


}

0 个答案:

没有答案