-1

I Have a Problem with the Camera Capture Image.its set blurry.

I searched lots of but i can't Get Solutions

I don't know How to solve issue

Here is My Code Which i Used For Camera Capture Image

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == SELECT_FILE)
            onSelectFromGalleryResult(data);
        else if (requestCode == REQUEST_CAMERA)
            onCaptureImageResult(data);
    }
}

private void onCaptureImageResult(Intent data) {
    Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    thumbnail.compress(Bitmap.CompressFormat.JPEG, 0, bytes);

    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".jpg");

    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }


  img1.setImageBitmap(thumbnail);
}

@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
    Uri selectedImageUri = data.getData();
    String[] projection = {MediaStore.MediaColumns.DATA};
    Cursor cursor = managedQuery(selectedImageUri, projection, null, null,
            null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    cursor.moveToFirst();

    String selectedImagePath = cursor.getString(column_index);

    Bitmap bm;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(selectedImagePath, options);
    final int REQUIRED_SIZE = 200;
    int scale = 1;
    while (options.outWidth / scale / 2 >= REQUIRED_SIZE
            && options.outHeight / scale / 2 >= REQUIRED_SIZE)
        scale *= 2;
    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(selectedImagePath, options);

    img1.setImageBitmap(bm);

}

The Gallery Image Set Good in Imageview But Its Only Happen with the CameraCapture Imageview Camera Capture Image is not clear(Blurry) in Android

Help Me for this issue.

Thanks In Advance

Community
  • 1
  • 1

3 Answers3

2

Your problem is here

thumbnail.compress(Bitmap.CompressFormat.JPEG, 0, bytes);

Second paramater in .compress() method is quality which can be in range 0..100. You have set it to 0 -> maximum compression.

Change the value to a higher value.

Harshil Pansare
  • 1,117
  • 1
  • 13
  • 37
1

What you are doing is getting thumbnail nail out of intent data that's why it is blurry

Try This : File mImageFile is path where you want to store you camera image file.

    mImageFile= new File(Environment.getExternalStorageDirectory() + File.separator + "DCIM" + File.separator + "temp.png"); 

Uri tempURI = Uri.fromFile(mImageFile);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
activity.startActivityForResult(i, CHOOSE_CAMERA_RESULT);


then in your onActivityResult

@Override
public void onActivityResultRAW(int requestCode, int resultCode, Intent data)
{
    super.onActivityResultRAW(requestCode, resultCode, data);
    switch(requestCode)
    {
    case CHOOSE_CAMERA_RESULT:
    {
        if (resultCode == RESULT_OK)
        {
            // here you image has been save to the path mImageFile.
            Log.d("ImagePath", "Image saved to path : " + mImageFile.getAbsoultePath());
        }
    }
    break;
    }
}
dex
  • 5,182
  • 1
  • 23
  • 41
  • i hava a Two Option Gallery or Camera SO How Can i Modified it.Help me to Modifi this.@dex – Rushang Prajapati Dec 11 '15 at 18:26
  • Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQUEST_CAMERA); I used this for Open Camera @dex – Rushang Prajapati Dec 11 '15 at 18:28
  • @RushangPrajapati : once you click on camera button , call startCamera Activity code , once click is over then in OnActivityResult you can get the image ...process accordingly. For gallery you can invoke gallery via Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/*"); startActivityForResult(intent, Gallery_code) , then in onActivityResult you will get URI , openInputStream from that uri to get the bitmap of image. – dex Dec 11 '15 at 18:32
  • mImageFile is path where you will store you capture image. for example mImageFile= new File(Environment.getExternalStorageDirectory() + File.separator + "DCIM" + File.separator + "temp.png"); means capture image will be store in inside /sdcard/DCIM/ folder. you can give any path. – dex Dec 11 '15 at 18:35
  • I Set All things But it shows "Bitmap too large to be uploaded into a texture (3120x4160, max=4096x4096)" How to Compress Bitmap ?? – Rushang Prajapati Dec 11 '15 at 18:38
  • @RushangPrajapati : you need to re-size your bitmap so that it can fit in 1024 * 1024 or what ever memory available. check this link http://stackoverflow.com/questions/4837715/how-to-resize-a-bitmap-in-android – dex Dec 11 '15 at 18:39
  • How can i Resize Bitmap ? @dex – Rushang Prajapati Dec 11 '15 at 18:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/97672/discussion-between-dex-and-rushang-prajapati). – dex Dec 11 '15 at 18:40
0

When you try to get bitmap from data.getExtras object in onActivityResult then it returns bitmap with low quality, below solution is complete followup Android Documentation. Don`t forget to write permission in Manifest.

lateinit var currentPhotoPath: String
var photoFile: File? = null
private fun takePhotoFromCamera() {
    Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        .also { takePictureIntent ->
            photoFile = try {
                createImageFile()
            } catch (ex: IOException) {
                null
            }

            photoFile?.also {
                fileUri = FileProvider.getUriForFile(
                    this, "com.example.fileprovider",
                    it
                )
                AppLogger.d("asad_fileUri", "$fileUri")
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri)
                startActivityForResult(takePictureIntent, CommonUtils.CAMERA)
            }
        }

}


override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    try {
        if (requestCode == CommonUtils.CAMERA) {
            galleryAddPic()
            setPic()?.let {
                fileUri = getImageUri(this, it)
            }
            postImagesAdapter.addItems(
                listOf(PostImagesModel(fileUri.toString(), false, fileUri)), false
            )

        } catch (e: Exception) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
            .show()
    }
    super.onActivityResult(requestCode, resultCode, data)
}

// File path xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="my_images"
        path="/" />
</paths>

//AndroidManifest
<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.example.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_path"></meta-data>
    </provider>

Take Photos

Asad Mukhtar
  • 391
  • 6
  • 18