1

我想在纵向模式下制作 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 {



        CameraActivity a=new CameraActivity();


        Camera.Parameters parameters = mCamera.getParameters();

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




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






        // Log.w("parameters"," "+  mCamera.getParameters().getSupportedPreviewSizes());


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



         Toast.makeText(getContext(), "width" +size.width, Toast.LENGTH_LONG).show();

         Toast.makeText(getContext(), "height" +size.height, Toast.LENGTH_LONG).show();

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


Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) {
    Camera.Size result=null;
    float dr = Float.MAX_VALUE;
    float ratio = (float)width/(float)height;

    for (Camera.Size size : parameters.getSupportedPreviewSizes()) {
        float r = (float)size.width/(float)size.height;
        if( Math.abs(r - ratio) < dr && size.width <= width && size.height <= height ) {
            dr = Math.abs(r - ratio);
            result = size;
        }
    }

    return result;
}

}

我也用过:-

    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;

}

但没有任何解决方案可以帮助我..请帮帮我..?

4

0 回答 0