Android: Taking picture with low quality [duplicate] - android

This question already has answers here:
Low picture/image quality when capture from camera
(3 answers)
Closed 3 years ago.
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.
And here is in preview camera mode after the image is captured.
If we compare these two photos, we can see the image in preview mode is blur.

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>

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

Related

Capturing and displaying the same image

I am working on a semester project and it requires that I send images from an android device to server without storing the image. I want to make sure my cameta application is working properly. I want to check this using an ImageView that displays the captured image on the screen. How can I do it? I thought I could load from drawable but I cannot add files to drawable during runtime.
Thanks for any support
You don't need to save it to the drawable. These are the two steps:
1.Convert the captured image to Bitmap
2.Set the bitmap image to an imageview
Ref: StackOverflow Answer
private void takePicture(){ //you can call this every 5 seconds using a timer or whenever you want
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 == Activity.RESULT_OK) {
Bitmap picture = (Bitmap) data.getExtras().get("data");//this is your bitmap image and now you can do whatever you want with this
imageView.setImageBitmap(picture); //for example I put bmp in an ImageView
}
}

Use of captured image bitmap outside onActivityResult method

I am taking photo from my activity using camera intent as shown in the below code, but i need to use the captured image bitmap outside the onActivityResult() method but this is not working, i tried to declare the bitmap outside the method and then use outside the method but this did not work, it seems that the bitmap did not catch the image in the onActivityResult, so can anyone help in order to use the bitmap of captured image outside the method or to save it and get its path like using the select image from gallery, so my goal is to use the bitmap of captured image for image processing purpose outside the onActivityResult() method, below is my code, thanks
captureButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_CAMERA_PERMISSION_CODE);
} else {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
});
and this the onActivityResult() method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageSelected.setImageBitmap(photo);
}
}
I tried this steps and it worked for me
1) define a Bitmap Object above of your onCreate method like bellow
Bitmap photo;
2) instead of Bitmap photo = (Bitmap) data.getExtras().get("data"); in onActivityResult write bellow code
photo = (Bitmap) data.getExtras().get("data");
and after that you can access to your photo object everywhere in your activity you like

Choose image from gallery, display in imageview and save image displayed in a new directory

I have two buttons and an ImageView in my Activity. When I click on one button it would redirect to gallery and I would be able to select an image. The selected image would appear in my ImageView.
The problem now is i want the save button to save the image displayed in a new directory like it does in apps like instagram.
My layout is given below;
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="imgClick"
android:id="#+id/imgButton"
android:text="Select Image"
android:layout_marginTop="20dp" />
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
android:layout_centerHorizontal="true"
android:id="#+id/diddyImageView"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:maxWidth="100dp"
android:maxHeight="100dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="addMovieClick"
android:id="#+id/addMovieButton"
android:text="Submit Movie"
android:background="#color/colorPrimary"
android:layout_marginTop="20dp" />
My activity is given below
public void imgClick(View v){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECTED_PICTURE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECTED_PICTURE && resultCode == RESULT_OK) {
uri = data.getData();
diddyImageView.setImageURI(uri);
}
}
The image is displayed properly, i want to save the image in a new folder and i dont know how to go about it..Any help will be appreciated..Thanks
Step #1: Use a ContentResolver and openInputStream() to open an InputStream on the Uri to the image
Step #2: Open a FileOutputStream on the desired file for your copy
Step #3: Use standard Java I/O to copy the bytes from the InputStream to the FileOutputStream
Step #4: Move all of that work into a background thread (and consider using an image-loading library to replace your setImageURI() call), to avoid tying up the main application thread doing all of this work

How to display Photograph without distortion in Android Application

My current Android Application allows the user to select any photograph they have on their device.
I use this code to display all photographs to allow user selection
final Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
Once the photograph has been selected, i display it within my applications main activity.
My main activity employs a LinearLayout as follows:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10" >
<ImageView
android:id="#+id/photograph"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/select_photo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:text="Photo Select" />
</LinearLayout>
I use this code to display the photograph in my application
#Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri imageUri = data.getData();
try {
final Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
The selected photographs are never dislayed correctly, e.g. they are distorted to a lesser or greater degree.
How do i guarantee that any photograph selected is displayed with the correct dimensions/proportions?
Either size your ImageView to "the correct dimensions/proportions", or use android:scaleType to indicate how you want the image scaled when it is applied to the ImageView.

image put into ImageView from taken picture not always showing up

An image taken from the camera doesn't always show up in my ImageView. It seems like it is random when it wants to show up. Why is it doing this?
Activity that takes picture
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_PICTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String spokenText;
// Handles picture taking after finished
if (requestCode == REQUEST_PICTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap image = (Bitmap) extras.get("data");
ImageView imageResult = (ImageView) findViewById(R.id.result_image);
imageResult.setImageBitmap(image);
ImageHandler imageHandler = new ImageHandler(this);
imageHandler.writeToFile(image, step.getChecklistId(), step.getOrder());
step.setImageFilename(imageHandler.getFilename(step.getChecklistId(), step.getOrder()));
finishStep();
}
}
XML to show picture
<ImageView
android:id="#+id/result_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:contentDescription="#string/taken_img_desc" />
Are you using notifyDataSetChanged for your adapter?
Is your view being refreshed? Try .invalidate();
One more thing: You might experience that the camera intent does not return an image or an uri to an image. I created a wrapper to help you with that. See the whole discussion on stackoverflow here or find the code on github.

Categories

Resources