使用自定义相机,图像质量很差

时间:2016-09-28 08:18:53

标签: android camera

我正在使用自定义相机并且工作正常,但问题是图像以非常低(差)质量保存。为了克服它,我已经尝试了所有的建议和实现。喜欢,

parameters.setJpegQuality(100);
parameters.setPictureFormat(ImageFormat.JPEG);

这不起作用。之后我用了

List<Size> sizes = cameraParams.getSupportedPictureSizes();
Camera.Size size = sizes.get(0);
for(int i=0;i<sizes.size();i++)
{
 if(sizes.get(i).width > size.width)
 size = sizes.get(i);
}
cameraParams.setPictureSize(mPictureSize.width, mPictureSize.height);

这也行不通。它的质量仍然很差。

注意:相机预览显示质量正常,但问题是将拍摄的图像保存到SD卡文件夹。

高级帮助将不胜感激!!

2 个答案:

答案 0 :(得分:5)

最后我的问题解决了。

在我拍摄图像之前,我在设置parameters进行相机预览

 public void takePicture() {
    mCamera.takePicture(new ShutterCallback() {
        @Override
        public void onShutter() {

        }
    }, new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

        }
    }, new PictureCallback() {
        @Override
        public void onPictureTaken(final byte[] data, Camera camera) {

            data1 = data;
            if (mCamera != null) {
                mCamera.stopPreview();
            }
        }
    });
}

所以在我在片段中调用此函数之前,我已经在此方法之前设置了参数。

mPreview.setParams(params);// This was the mistake what i have done !
mPreview.takePicture();

在删除mPreview.setParams(params);

后终于解决了

答案 1 :(得分:1)

我可以向您展示重置预览尺寸的方法:

您应该在相机处更改预览参数。

private void setImageSize() {
        Camera.Size size = CameraUtil.findClosetImageSize(mRxCamera.getNativeCamera(), PREFER_SIZE);
        mCamera.getNativeCamera().getParameters().setPictureSize(size.width, size.height);
        WIDTH = size.width;
        HEIGHT = size.height;
    }

之后您需要更改布局尺寸

private void resetPreviewSize() {
        final boolean widthIsMax = mWidth > mHeight;
        final Camera.Size size = mCamera.getNativeCamera().getParameters().getPreviewSize();

        final RectF rectDisplay = new RectF();
        final RectF rectPreview = new RectF();

        rectDisplay.set(0, 0, mWidth, mHeight);

        final int width = widthIsMax ? size.width : size.height;
        final int height = widthIsMax ? size.height : size.width;
        rectPreview.set(0, 0, width, height);

        final Matrix matrix = new Matrix();
        matrix.setRectToRect(rectDisplay, rectPreview, Matrix.ScaleToFit.START);
        matrix.invert(matrix);
        matrix.mapRect(rectPreview);

        mCameraView.getLayoutParams().height = (int) (rectPreview.bottom);
        mCameraView.getLayoutParams().width = (int) (rectPreview.right);
        mCameraView.requestLayout();
    }

如果你需要

public static Camera.Size findClosetImageSize(Camera camera, Point preferSize) {
    int preferX = preferSize.x;
    int preferY = preferSize.y;
    Camera.Parameters parameters = camera.getParameters();
    List<Camera.Size> allSupportSizes = parameters.getSupportedPictureSizes();
    Log.d(TAG, "all support Image size: " + dumpPreviewSizeList(allSupportSizes));
    int minDiff = Integer.MAX_VALUE;
    int index = 0;
    for (int i = 0; i < allSupportSizes.size(); i++) {
        Camera.Size size = allSupportSizes.get(i);
        int x = size.width;
        int y = size.height;

        int diff = Math.abs(x - preferX) + Math.abs(y - preferY);
        if (diff < minDiff) {
            minDiff = diff;
            index = i;
        }
    }
    return allSupportSizes.get(index);
}