压缩自定义相机拍摄的图像尺寸

时间:2013-12-09 09:05:28

标签: android bitmap camera compression resolution

如何压缩 自定义相机 拍摄的图像尺寸

获取 3264 x 2448像素 图像分辨率

必需 1024 x 768像素 图片分辨率

我希望每当用户使用自定义相机拍照时,它应该是 所需尺寸(1024 x 768像素)

注意:默认情况下,在模拟器上我得到( 640 X 480像素),而在设备上(3264 x 2448)像素)

CameraActivity: -

 private Camera mCamera;

    private PreviewSurface mCameraPreview;

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_camera);

                 mCamera = getCameraInstance();

                    mCameraPreview = new PreviewSurface(this, mCamera);
                    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
                    preview.addView(mCameraPreview);

                    Button captureButton = (Button) findViewById(R.id.btnCapture);
                    Log.d(CameraLauncherActivity.LOG_TAG, "captureButton :: " + captureButton);
                    captureButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mCamera.takePicture(null, null, mPicture);
                            Log.d(CameraLauncherActivity.LOG_TAG, "mCamera.takePicture :: " + mCamera);                                         
                        }
                    });

    private Camera getCameraInstance() {

            Camera camera = null;
            Log.d(CameraLauncherActivity.LOG_TAG, "getCameraInstance()Camera:: " + camera);
            try {
                camera = Camera.open(0);
                Log.d(CameraLauncherActivity.LOG_TAG, "getCameraInstance()open:: " + camera);
            } catch (Exception e) {
                // cannot get camera or does not exist
            }
            return camera;
        }
    }

PreviewSurface.java: -

public PreviewSurface(Context context, Camera camera) {
            super(context);
            this.mCamera = camera;

            this.mSurfaceHolder = this.getHolder();
            this.mSurfaceHolder.addCallback(this);
            this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            this.mSurfaceHolder.setFixedSize(100, 100);
        }

        @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 {
                mCamera.setDisplayOrientation(90);
                mCamera.setPreviewDisplay(surfaceHolder);
                mCamera.startPreview();
                } catch (Exception e) {
                // intentionally left blank for a test
            }
        }

1 个答案:

答案 0 :(得分:1)

并非所有相机都支持所有预览尺寸。您需要将摄像机的预览大小设置为尽可能接近目标分辨率。

使用mCamera.getParameters().getSupportedPreviewSizes();获取支持的屏幕尺寸并使用:

Camera.Parameters parameters = mCamera.getParameters();
    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);

设置预览大小。如果您需要精确的尺寸,可以压缩结果图像。

更新

私人相机getCameraInstance(){

    Camera camera = null;
    Log.d(CameraLauncherActivity.LOG_TAG, "getCameraInstance()Camera:: " + camera);
    try {
        camera = Camera.open(0);
        Log.d(CameraLauncherActivity.LOG_TAG, "getCameraInstance()open:: " + camera);
    } catch (Exception e) {
        // cannot get camera or does not exist
    }
    if(camera!=null){
     //get supported sizes:
     List<Size> localSizes = camera.getParameters().getSupportedPreviewSizes();
     //loop through localSizes to get closest size
     Camera.Parameters parameters = camera.getParameters();
     parameters.setPreviewSize(/*closest you could find*/);
     camera.setParameters(parameters);
    }
    return camera;
}

}