Android Camera setPictureSize parameter invokes line art

I am trying to take a picture using an Android camera. I have a requirement to capture an image of 1600 (w) x 1200 (h) (a third-party requirement). My code seems to work fine for many phone cameras, but setPictureSize crashes on some phones (Samsung Galaxy S4, Samsung Galaxy Note) and causes a series of images on others (Nexus 7 tablet). At least the Nexus size that I wish is displayed in the getSupportPictureSizes list.

I tried to indicate the orientation, but that did not help. Shooting with the default image size works fine.

Here is an example of bands:

enter image description here

For my image capture, I have a requirement of 1600x1200, jpg, 30% compression, so I take a JPG file.

I think I have three options: 1) Learn how to capture a size of 1600x1200 without glitches or stripes, or 2) Look at how to resize the default image size to JPG, which is 1600x1200. 3) Something else that I do not know.

I found several other posts that have similar problems, but not exactly the same. I am on my second day of trying, but I cannot find a solution. Here is one message that is close:

A camera image for a bitmap results in a damaged image (none of the suggestions helped me)

Here is a section of my code that worked fine until I came across S4 / Note / Nexus 7. At the moment, I have added some code for debugging:

Camera.Parameters parameters = mCamera.getParameters();
Camera.Size size = getBestPreviewSize(width, height, parameters);

if (size != null) {
    int pictureWidth = 1600;
    int pictureHeight = 1200;

    // testing
    Camera.Size test = parameters.getPictureSize();
    List<Camera.Size> testSizes = parameters.getSupportedPictureSizes();
    for ( int i = 0; i < testSizes.size(); i++ ) {
        test = testSizes.get(i);
    }

    test = testSizes.get(3);
    // get(3) is 1600 x 1200
    pictureWidth = test.width;
    pictureHeight = test.height;

    parameters.setPictureFormat(ImageFormat.JPEG);
    parameters.setPictureSize(pictureWidth, pictureHeight);
    parameters.setJpegQuality(30);
    parameters.setPreviewSize(size.width, size.height);

    // catch any exception
    try {
        // make sure the preview is stopped
        mCamera.stopPreview();

        mCamera.setParameters(parameters);
        didConfig = true;
    catch(Exception e) {
        // some error presentation was removed for brevity
        // since didConfig not set to TRUE it will fail gracefully
    }
}

Here is the section of my code that saves the jpg file:

PictureCallback jpegCallback = new PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {
        if ( data.length > 0 ) {

            String fileName = "image.jpg";

            File file = new File(getFilesDir(), fileName);
                String filePath = file.getAbsolutePath();

             boolean goodWrite = false;
             try {
                 OutputStream os = new FileOutputStream(file);
                 os.write(data);
                 os.close();

                goodWrite = true;
            } catch (IOException e) {
                 goodWrite = false;
             }

             if ( goodWrite ) {
                // go on to the Preview
             } else {
                // TODO return an error to the calling activity
             }

        }

        Log.d(TAG, "onPictureTaken - jpeg");
    }
};

, , . , ( API 8 )! , .

!

: :

Camera.Parameters getSupportedPictureSizes, , , , , . .

, :

BitmapFactory.Options options = new BitmapFactory.Options();;
options.inPurgeable = true;

// convert the byte array to a bitmap, taking care to allow for garbage collection
Bitmap original = BitmapFactory.decodeByteArray(input , 0, input.length, options);
// resize the bitmap to my desired scale 
Bitmap resized = Bitmap.createScaledBitmap(original, 1600, 1200, true);

// create a new byte array and output the bitmap to a compressed JPG
ByteArrayOutputStream blob = new ByteArrayOutputStream();
resized.compress(Bitmap.CompressFormat.JPEG, 30, blob);

// recycle the memory since bitmaps seem to have slightly different garbage collection
original.recycle();
resized.recycle();

byte[] desired = blob.toByteArray();

jpg .

+4
1
test = testSizes.get(3);
// get(3) is 1600 x 1200

, 4+ , , 1600x1200.

1) , 1600x1200 .

, . - . , ( ) .

2) , JPG, 1600x1200

, " ", , , , . - №1 .

3) - , .

, , , , 1600x1200.

, , (, 4: 3 ..), /, 1600x1200.

+4

Source: https://habr.com/ru/post/1535048/


All Articles