[英]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?
1,在布局中包含一個帶有CropImageView
的Activity
2.使用帶有startActivityForResult
的Intent
啟動相機活動以拍照。
3.在Activity中,重寫onActivityResult
以獲取圖片位圖並執行`CropImageView.setImageBitmap(bitmap)和瞧! 現在您可以裁剪了...
以這個為例: 從相機捕獲圖像並在活動中顯示
試用以下代碼:
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);
以下參數起着至關重要的作用:
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
有一個適合您的活動的xml文件,您將在其中顯示原始圖像在main_imageview中,並將另一個圖像視圖作為子級添加到crop_main_layout中,並以scro rect圖像作為src(9個補丁圖像)
<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>
裁剪矩形圖像視圖可以是源自Imageview的自定義類,該類將處理所有onTouchEvents,並提供調整大小/移動功能,一旦用戶選擇了所選內容並按裁剪屏幕上的完成,就使用選定的裁剪矩形(自定義裁剪)裁剪圖像矩形圖像視圖范圍)。
Bitmap.createBitmap(currentBitmap, cropRect.left, cropRect.top, cropRect.width(), cropRect.height());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.