Android Poor image quality when saving image with takePicture callback

I looked at a bunch of other stack answers regarding bad images from an Android camera, but they all look from people who grabbed a thumbnail and then scaled it.

My problem is that even the image stored in the library comes out with very poor quality. It comes out with dull colors and more pixelated.

Here is my callback where I save the image in the library. Do you see why I lose image quality when I save it?

Here is the callback

  @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.write(data);
        fos.close();
        MediaScannerConnection.scanFile(getBaseContext(), new String[]{pictureFile.getPath()}, new String[]{"image/png"}, null);

This is where I get outputMediaFile

private static File getOutputMediaFile(int type){
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "PumpUp");
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE){
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "IMG_"+ timeStamp + ".png");
            Log.d(DEBUG_TAG,mediaStorageDir.getPath() + File.separator + "IMG_"+ timeStamp + ".png");
        } else if(type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "VID_"+ timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

UPDATE:

The bitmap data gives me a width and height of 320. Do you know why this has decreased?

UPDATE 2:

This code prints 320W 240H

private Camera.PictureCallback mPicture = new Camera.PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
            Bitmap imageBitmap = BitmapFactory.decodeByteArray(data,0,data.length);
            Log.d(DEBUG_TAG,imageBitmap.getWidth() + "  h " + imageBitmap.getHeight());
}

, ?

+4
3

- , .

URI EXTRA_OUTPUT

 //create file at path where you like
 File photo = new File();
 URI resultUri = Uri.fromFile(photo);
 intent.putExtra(MediaStore.EXTRA_OUTPUT, resultUri);
 //this resultUri would be  your final image
0

dev , :

Note. This miniature image from the "data" may be useful for the icon, but not much more. Working with a full-sized image requires a bit more work.

To get a full-size photo, you need to do it as follows:

The following is the method you need to call to start your camera:

static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(photoFile));
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}    

Here is a way to create a new file with a unique name:

String mCurrentPhotoPath;

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}
0
source

All Articles