问题
I use this code to start the crop activity and i get the cropped picture as data in onActivityResult back. So this works fine.
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities(
intent, 0);
int size = list.size();
if (size == 0) {
Toast.makeText(this, "Can not find image crop app",
Toast.LENGTH_SHORT).show();
return;
} else {
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
This is the code for cropping the image into the size of 200x200, dont matter which aspect ratio I chose at the cropping activity. My concern is, that i want THE aspect ratio which i choose with the rectangle in the activity, not a fixed by putting the numbers 200 and 200.
But when I comment out these two lines, my program will force close....
Are there any solutions for cropping exact to the part of the picture which i chose in the crop activity with keeping the aspect ratio from the rectangle I placed on my picture? Which data do I have to put as extras? Need Help!
回答1:
How to crop a rectangle from an image in android
Cropping to anything other than a square shape is not possible using the built-in Android cropping handling (com.android.camera.action.CROP).
Look at the OP's reply in the next thread - and you might have a solution.
This thread also has some good suggestions:
- How to select and crop an image in android?
回答2:
Just add the following lines of code for aspect ratio 0.75
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 4);
and set scale to true
.
this should solve the problem.
回答3:
It might be little late to answer, considering that the other people might come to this thread with this same question.
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
will make sure that you always get a rectangle.
回答4:
Selecting a image from gallery and cropping keeping Aspect Ratio wanted. In this case the ratio is BANNER_WIDTH_PX:BANNER_HEIGHT_PX.
protected static final int REQ_CODE_PICK_BANNER = 2; //identificador del inten
private static final int BANNER_WIDTH_PX = 640;
private static final int BANNER_HEIGHT_PX =244;
protected void startSelectorBanner(){
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra("aspectX",BANNER_WIDTH_PX);
photoPickerIntent.putExtra("aspectY",BANNER_HEIGHT_PX);
photoPickerIntent.putExtra("outputX",BANNER_WIDTH_PX);
photoPickerIntent.putExtra("outputY",BANNER_HEIGHT_PX);
photoPickerIntent.putExtra("return-data", true);
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent, REQ_CODE_PICK_BANNER);
}
public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
getMainActivity().removeOnActivityResultsListeners(EditQcardFragment.this);
switch (requestCode) {
case REQ_CODE_PICK_BANNER:
if (resultCode == Activity.RESULT_OK) {
if (imageReturnedIntent!=null) {
Bundle extras = imageReturnedIntent.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
int[] screenSize = Screen.getSize(getActivity());
int bannerX = screenSize[0];
int bannerY = (int) (screenSize[0]/BANNER_RATIO);
Bitmap resizedBitmap = processPicture(selectedBitmap,"banner.png",bannerX,bannerY);
mBytesBanner = getPictureBytes("banner.png", resizedBitmap);
log.debug("Get png width: " + mBytesBanner.length);
if(mBytesBanner!=null){
mBanner.setImageBitmap(resizedBitmap);
}
}
}break;
}
}
来源:https://stackoverflow.com/questions/10453446/cropping-pictures-in-android-by-keeping-aspect-ratio