Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image quality is poor using custom camera

Tags:

android

camera

I am using custom camera and working fine but the issue is image is saving with very low (poor) quality. To overcome with it , i have tried all suggestions and implementations. Like ,

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

this is not working. After that i have used

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);

This is also not working. Its saving with poor quality still.

Note : Camera preview is showing proper with good quality but the issue is when saving captured image to sdcard folder.

Advanced help would be appreciated!!

like image 427
Piyush Avatar asked Sep 28 '16 08:09

Piyush


People also ask

What affects image quality in a camera?

While sensor type and size, megapixel count and pixel size are all important when it comes to image quality, there are other, often overlooked, factors such as lens choice, file type, and the setting combinations used to capture the image that also have an impact.

What is the best setting for image quality?

'High' or 'Fine' gives the best quality but the biggest files, 'Medium' or 'Normal' gives decent quality but smaller files, while 'Low' or 'Basic' means very small files but a visible quality loss. We always recommend 'Fine' quality for JPEGs – the files are bigger, but heck, you can just buy a bigger memory card.

What is image quality on a camera?

“Image quality” refers to the file type and compression ratio used when images are saved, while “image size” (measured in pixels) determines the physical dimensions of the image.


2 Answers

Finally my issue solved.

Here I was setting parameters for camera preview before i was capturing the image

 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();
            }
        }
    });
}

So before i called this function in my fragment i have set parameters before this method.

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

finally solved after removing mPreview.setParams(params);

like image 167
Piyush Avatar answered Sep 18 '22 23:09

Piyush


I can show you methods for resetting preview size:

You should change your parameters of preview at Camera.

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

and after You need to change layout sizes

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();
    }

and if you need

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);
}
like image 40