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