Kaveri Patel
Kaveri Patel

Reputation: 27

Captured image displays blur

I am trying to capture image from camera but it come out to be blur. Below is the code that I am using.

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  startActivityForResult(intent, CAMERA_REQUEST);

  Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
  mPhotoEditorView.getSource().setImageBitmap(thumbnail);

Upvotes: 0

Views: 1972

Answers (3)

Amit Kumar Sahu
Amit Kumar Sahu

Reputation: 1

You can scale bitmap with your desired height and width high value to get more clearity.

first you have to get path from your intent data uri.

private String getpath(Context context, Uri uri) {
    String filePath = null;
    try {
        Cursor cursor = context.getContentResolver().query(uri, null, null,
                null, null);
        if (cursor != null) {
            if (cursor.moveToNext()) {
                int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                filePath = cursor.getString(dataColumn);
            }
            cursor.close();
        }

    } catch (IllegalStateException e) {

    }
    return filePath;
}

then use this code to get your image bitmap

public static Bitmap scaleImage(String p_path, int p_reqHeight, int p_reqWidth) throws Throwable {
    Bitmap m_bitMap = null;

    File m_file = new File(p_path);
    if (m_file.exists()) {
        BitmapFactory.Options m_bitMapFactoryOptions = new BitmapFactory.Options();
        m_bitMapFactoryOptions.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
        m_bitMapFactoryOptions.inSampleSize = calculateInSampleSize(m_bitMapFactoryOptions, p_reqHeight, p_reqWidth);
        m_bitMapFactoryOptions.inJustDecodeBounds = false;
        m_bitMap = BitmapFactory.decodeFile(m_file.getPath(), m_bitMapFactoryOptions);
    } else {
        throw new Throwable(p_path + " not found or not a valid image");
    }
    return m_bitMap;
}

Upvotes: 0

Lakshmi Priya
Lakshmi Priya

Reputation: 11

Try to use following code to resolve blur issue

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

Upvotes: 0

Deˣ
Deˣ

Reputation: 4371

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

Above method only provides thumbnail.

To save the full-size Photo you should follow this tutorial.

Add this permission in your manifest.

<manifest ...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                     android:maxSdkVersion="18" />
    ...
</manifest>

Create a File. In this, we will save the image:

String currentPhotoPath;

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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    currentPhotoPath = image.getAbsolutePath();
    return image;
}

Now, you can invoke capture Intent like this:

static final int REQUEST_TAKE_PHOTO = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's 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) {
            Uri photoURI = FileProvider.getUriForFile(this,
                                                  "com.example.android.fileprovider",
                                                  photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
        }
    }
}

Add FileProvider in your Manifest file

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

Create res/xml/file_paths.xml file with the following content:

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

Here you go, you have saved a full-size image to the file you had created.

Bonus: You should always scale your image before using it in an ImageView, this will help you optimize your app's Memory usage.

private void setPic() {
    // Get the dimensions of the View
    int targetW = imageView.getWidth();
    int targetH = imageView.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
    imageView.setImageBitmap(bitmap);
}

Upvotes: 1

Related Questions