繁体   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