简体   繁体   中英

I want to crop an image and type was view in image. How can I crop an image from camera using this type of cropping?

How can I crop a image? I have tried some concepts using intents but still failed.. I want this type of cropping.

Here's a image:

1.Have an Activity with your CropImageView in it's layout.
2.Start the camera activity with an Intent with a startActivityForResult to take the picture.
3.In the Activity, override onActivityResult to take the picture bitmap and do `CropImageView.setImageBitmap(bitmap) and voila! Now you can crop it...
Have a look at this one for an example: Capture Image from Camera and Display in Activity

Try out following code:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 1; 
Bitmap bm = BitmapFactory.decodeFile(imagePath, opts);
Intent intent = new Intent("com.android.camera.action.CROP");              
intent .setDataAndType(outputFileUri, "image/*");
intent.putExtra("outputX",bm.getWidth());
intent.putExtra("outputY", bm.getHeight());
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("scale", true);
intent.putExtra("return-data", false); 
startActivityForResult(intent, 2);

following parameters play vital role:

 intent.putExtra("aspectX", 0);
    intent.putExtra("aspectY", 0);

For more info: Android - Crop image taken from camera with resizable ,independant height and width of cropped area

Have a xml file for your activity where you will display the original image in the main_imageview and add another imageview as child to the crop_main_layout having the crop rect image as src (9 patch image)

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/crop_main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/imageview_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >
        <ImageView
            android:id="@+id/main_imageview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/transparent"
            android:scaleType="centerInside"
            android:padding="@dimen/crop_view_padding"/>
    </LinearLayout>
</RelativeLayout>

The crop rect image view can be a custom class deriving from Imageview which will handle all onTouchEvents and will provide resize/move functionality and once user selects the selection and presses Done on your crop screen, crop the image using the selected crop rect (custom crop rect image view bounds).

Bitmap.createBitmap(currentBitmap, cropRect.left, cropRect.top, cropRect.width(), cropRect.height());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM