Android: Taking picture with low quality

2019-06-14 10:23发布

I created a project that allow user to take a picture and view it. I have no face any issue while taking, view, save and retrieve the photo. But my problem is the image that camera took is in bad quality, I don't know how to set the quality of the camera Intent. Here is my code looks like.

....
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
....

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

        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            Bitmap userPhoto = (Bitmap) data.getExtras().get("data");
            iv_user.setImageBitmap(userPhoto);
        }
    }

Here is a screenshot of while camera is on.

enter image description here

And here is in preview camera mode after the image is captured.

enter image description here

If we compare these two photos, we can see the image in preview mode is blur.

2条回答
Explosion°爆炸
2楼-- · 2019-06-14 10:55

Im actually not sure why it's like that but it's same here after every method tried, so I suggest the solution that you make use of a very simple library like below, good things is your picture quality remains intact.

simple add to your gradle file:

    implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'

Simple call startCameraAndOptions(); method to start you camera

    startCameraAndOptions(); //call to starts you camera with other additional option like gallery.

    public void startCameraAndOptions(View view) {
            CropImage.activity()
                    .setGuidelines(CropImageView.Guidelines.ON)
                    .setActivityTitle("My Crop")
                    .setCropShape(CropImageView.CropShape.RECTANGLE)
                    .setCropMenuCropButtonTitle("Done")
    //                .setRequestedSize(400, 400)
                    .setAllowRotation(true)
                    .setOutputCompressQuality(100)
                    .setCropMenuCropButtonIcon(R.drawable.arrow)
                    .start(this);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            BitmapDrawable background;
            Bitmap bitmap;
            ImageView img_preview = (ImageView) findViewById(R.id.img_preview);

            // handle result of CropImageActivity
            if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
                CropImage.ActivityResult result = CropImage.getActivityResult(data);
                if (resultCode == RESULT_OK) {
                    bitmap = null;
                    try {
                        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), result.getUri());
                        background = new BitmapDrawable(bitmap);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    Picasso.get()
                            .load(result.getOriginalUri())
    //                        .resize(200, 200)
    //                        .centerCrop()
                            .into(img_preview);

                } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
                    Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
                }
            }
        }

**/* LAYOUT SECTION */ optional (style it your own way)**

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.hollaport.hollaport.Demo">

    <Button
        android:layout_marginTop="10dp"
        android:layout_gravity="center"
        android:id="@+id/trigger"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@drawable/camera"/>

    <!--<com.theartofdev.edmodo.cropper.CropImageView-->
        <!--android:id="@+id/cropImageView"-->
        <!--android:layout_width="match_parent"-->
        <!--android:layout_height="0dp"-->
        <!--android:layout_weight="1"/>-->

    <android.support.v7.widget.CardView
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:innerRadius="0dp"
        android:shape="rectangle"
        android:thicknessRatio="2"
        app:cardCornerRadius="0dp">

        <ImageView
            android:id="@+id/img_preview"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"/>
    </android.support.v7.widget.CardView>
</LinearLayout>
查看更多
做自己的国王
3楼-- · 2019-06-14 11:11

The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap (we call it thumbnail) in the extras, under the key "data".

If you want to retrive full size image your app first need to store image to external storage and then read it back. You can access full code at

https://developer.android.com/training/camera/photobasics

查看更多
登录 后发表回答