I can't change android camera resolution

2019-07-24 12:28发布

问题:

This is my code in onPreviewFrame Method.

The frame that shows on the surfaceHolder is fine.

I Already set the resolution with this code at first

 mCameraParameter = mCamera.getParameters();
 mCameraParameter.setPreviewSize(100,150);
 mCameraParameter.setPreviewFrameRate(20);
 mCameraParameter.setPreviewFormat(PixelFormat.JPEG);
 mCamera.setParameters(mCameraParameter);

but the picture that image get is 640 * 480

I wonder why i can't change the preview resolution.

        public void onPreviewFrame(byte[] data, Camera camera) {
        Log.e("PreviewCallBack", "Preview");


        Camera.Parameters parameters = camera.getParameters();
        Log.e("Picture Size", "width : " + parameters.getPreviewSize().width);
        Log.e("Picture Size", "height : " + parameters.getPreviewSize().height);
        Log.e("Array Size", "data.length : " + data.length);
        YuvImage image = new YuvImage(data, parameters.getPreviewFormat(),
                parameters.getPreviewSize().width,parameters.getPreviewSize().height, null);


        File file = new File(Environment.getExternalStorageDirectory()
                .getPath() + "/"+/*System.currentTimeMillis()*/"out.jpg");

        FileOutputStream filecon = null;

        try {
            filecon = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }


        image.compressToJpeg(
                new Rect(0, 0, image.getWidth(), image.getHeight()), 90,
                filecon);
    }
};

回答1:

Maybe you are trying to set an unsupported preview size. Better call "getSupportedPreviewSizes()" and check if the preview size you want to set is supported by your device before calling setPreviewSize().



回答2:

I think you should call setPictureSize() to set the size of the picture itself (not the preview).