簡體   English   中英

我想裁剪圖像,然后輸入圖像中的視圖。 如何使用這種裁剪方式從相機中裁剪圖像?

[英]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?

如何裁剪圖像? 我已經嘗試過使用Intent進行一些概念設計,但是仍然失敗。

這是一張圖片:

1,在布局中包含一個帶有CropImageView的Activity
2.使用帶有startActivityForResultIntent啟動相機活動以拍照。
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);

有關更多信息: Android-通過可調整大小的,獨立於裁剪區域的高度和寬度的相機拍攝圖像

有一個適合您的活動的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM