I am using the SurfaceHolder.Callback and Camera.PictureCallback to open Camera in my activity After taking image i want to crop that image and then save it to sd card. I found many solution for cropping via intent camera but didn't find any solution as per my Requirement.
I'll be thankful if someone help me.
Thanks in Advance.
You should implement Camera.PreviewCallback instead of Camera.PictureCallback,
by using the interface implemented method:
public void onPreviewFrame(byte[] data, Camera camera) {
if (shutterEnabled){
shutterEnabled = false;
// Process data
// Put it on a Bitmap
// Send it to cropImage
// -----
}
}
Take in account that if you didn't set on your camera parameters, setPreviewFormat, the picture on viewPreviewFrame would arrive as YUV (NV21) format.
Actually I'm looking how to make this part, this is why I bumped into your question.
The point here, how are you going to pass the picture to the crop method, you should look into that.
The shutterEnabled flag, it's enabled from your listener that 'watches' for that event.
private void cropImage( picture){
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picture, "image/*");
cropIntent.putExtra("aspectX",0);
cropIntent.putExtra("aspectY",0);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CAMERA_CROP_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK && requestCode == CAMERA_CROP_CODE){
Bitmap bmp = (Bitmap)data.getExtras().get("data");
}
}
Related
This question already has answers here:
Low picture/image quality when capture from camera
(3 answers)
Closed 3 years ago.
I want the good quality image to show in ImageView and upload to the server. I have searched the Internet, StackOverflow and GitHub, but I could not find the answer, maybe somebody knows how can fix it?
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { //work android
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
imageReturnedIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//try
if (resultCode == RESULT_OK ) {// work every android than need click OK
//working
Uri selectedImage = imageReturnedIntent.getData();//convert to for bitmap that can send
Bundle extras = imageReturnedIntent.getExtras();//finish converting and copy the image
bitmap = extras.getParcelable("data");//receive image to bitmap
imageView.setImageBitmap(bitmap);
onCreate:
btnCamera.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 0);
}
});
What is wrong here?
The Intent you send returns only low-quality picture as a small bitmap.
If you want the full-resolution image, you need to prepare the file where this image will be saved and provide all necessary information to the Camera application. The details are available at Save the full-size photo section, with all explanations and the code samples.
I'm trying to let the user:
Click on an ImageView
Pick an Image
Crop the Image
Finally display the cropped Image in the ImageView
So far it kinda works, but I've got a few issues:
After choosing the Image, the User gets asked AGAIN which program he wants to use for cropping. I don't want that
The cropping intent displays the area-to-crop as a circle. I'd rather have a square!
Thanks for any help guys, here's the complete class:
public class settings extends Activity {
ImageView masterUserImg;
private static int LOAD_IMAGE_RESULTS = 1;
final int PIC_CROP = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.settings);
masterUserImg=(ImageView)findViewById(R.id.masterUserImage);
masterUserImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Create the Intent for Image Gallery.
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start new activity with the LOAD_IMAGE_RESULTS to handle back the results when image is picked from the Image Gallery.
startActivityForResult(i, LOAD_IMAGE_RESULTS);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri pickedImage = null;
// 1) PICK IMAGE
if (requestCode == LOAD_IMAGE_RESULTS && resultCode == RESULT_OK && data != null) {
pickedImage = data.getData();
}
// 2) CROP IMAGE
performCrop(pickedImage);
// 3) LOAD IMAGE
if (requestCode == PIC_CROP) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
masterUserImg.setImageBitmap(selectedBitmap);
}
}
}
private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
catch (Exception ex) {
// display an error message
String errorMessage = "Crop failed";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
Please try this Cropper Library . It will fulfill your requirement.
It supports features which you needed .
For not prompt activity for cropping, you can try query activities for CROP intent and use one of them. For querying activities available for certain intent, please consult the document.
For the circle shape of crop area, it's the implementation of the activity which is handling the crop intent, there's no way to change that if you don't known how to configure it with correct extras parameters.
But as #CommonsWare said, there's no com.android.camera.action.CROP intent, there will be phones have no activity to handle that.
So you should use a crop library to handle that, like android-crop.
Finally, for your requirement, you can try droid-image-picker which let you select image from gallery and camera, and also integrate android crop for the cropping part.
Disclosure: I'm the developer of droid image picker
I'm trying to do an app (with the master/detail flow template) that in the detail fragment, has an ImageView that shows an image taken by the camera.
For this option, I added a button to the ActionBar that opens a Dialog box and asks the user if he wishes to add an image with the camera. So for this option I made this two methods that are called when the user clicks ok. However, (here I now I'm making huge mistakes) I don't have the least idea about how to call the onActivityResult() with the end of having my ImageIcon showing the thumbnail retrieved from the camera. (as developer.android tutorial shows)
If anyone of you guys knows what I am doing wrong, I would be really happy to hear all the solutions that you can give me, since I wasn't able to find a solution to this problem anywhere.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
onActivityResult(REQUEST_IMAGE_CAPTURE, RESULT_OK, takePictureIntent);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ImageView mImageView = (ImageView) findViewById(R.id.imageView1);
mImageView.setImageBitmap(imageBitmap);
}
}
THANKS A LOT!!!!
You do not have to call onActivityResult explicitly. onActivityResult method called in the parent activity when the immediately child/successor activity finished its task. So when your Image taking activity will finished the task it call automatically a "requestCode" and Image data(If your Image taking activity take a picture otherwise it will be null).
For more details about the onActivityResult see the here.
To set the thumbnail to the imageview you can do this in the following way.
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
You can see this tutorial about the how to take Image using intent.
Happy Coding
You have to create a scaled bitmap from the bitmap received in onActivityResult. You will get information about how to create a scaled bitmap Here
You can set the width and height as desired
imageBitmap = Bitmap.createScaledBitmap(realImageBitmap, final_width, final_height, false);
imageView.setImageBitmap(imageBitmap);
Okay, so I have an onclick on a button that opens the camera, when I take a picture and save it how do I get the file name+location of where it was saved to open it in my app?
Here is my button click
Button btncamera = (Button)findViewById(R.id.btncamera);
btncamera.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent cameraIntent = nwe Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 2500);
}
}
Here is my Result it doesn't contain anything, but it will start another Intent to display the image:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
}
Maybe I am doing it wrong, but any guidance would be much appreciated thanks!
Try adding this to your onActivityResult.
Bitmap picture = (Bitmap) data.getExtras().get("data");
Take the image out of the camera intent, create a bitmap out of it, and then use it from there. Write it to a file, or import it directly into a display resource.
Also be sure to check your ResultCode to determine if a picture actually has been taken.
in my app i m calling built in camera for capturing picture but i want to set that picture into an image view following is the code . so what code should i add to it to set the picture into imageview.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
String result = data.toURI();
// ...
}
}
Thanks in advance
Add a button and an ImageView to your main layout. When you click the button, first intent.putExtra to specify where the image will be stored, then start the camera intent. In your onActivityResult, if the resultCode comes back 0 that means the user accepted the picture. In this case, go grab the image from the path you specified as an extra of the intent. Create a bitmap from the file specified by the path, then set the image bitmap in your ImageView.