如何为camera2 API创建圆形TextureView

时间:2019-02-26 10:31:09

标签: android api android-camera2 textureview

我正在TextureView上创建一个遮罩布局,但是它不能满足我的要求,因为我想稍后将捕获的图像转换为圆形。有什么方法可以在Android中创建圆形TextureView吗?

2 个答案:

答案 0 :(得分:1)

在解决了许多变通办法之后。

创建形状并将其放置在可绘制文件夹中

     <?xml version="1.0" encoding="utf-8"?>
         <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
        </shape>

    </item>
    <item>
        <shape
            android:innerRadiusRatio="2"
            android:shape="ring"
            android:thicknessRatio="1"
            android:useLevel="false" >
            <solid android:color="#80000000" />
            <size
                android:height="148dip"
                android:width="148dip" />

        </shape>

    </item>

</layer-list>

然后将上面创建的形状设置为xml文件中的蒙版层的背景,如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_gravity="center"
    android:gravity="center"
    android:background="@color/background_color"
    android:layout_height="match_parent">



    <RelativeLayout

        android:orientation="vertical"
        android:background="@drawable/ring_shape"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="2dp"
        android:id="@+id/view2"
        android:layout_centerHorizontal="true"
        android:innerRadius="@dimen/pad2dp">

        <LinearLayout
            android:weightSum="1"
            android:layout_marginBottom="20dp"
            android:id="@+id/bottomLayout"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <ImageView
                android:foreground="@drawable/white_ripple"
                android:clickable="true"
                android:layout_weight="0.30"
                android:layout_gravity="center"
                android:id="@+id/flashCamera"
                android:layout_width="0dp"
                android:layout_height="@dimen/pad30dp"
                android:layout_alignParentBottom="true"
                android:src="@drawable/flash"
                android:text="Capture Image"
                android:visibility="invisible" />

            <ImageView
                android:layout_gravity="center"
                android:foreground="@drawable/white_ripple"
                android:clickable="true"
                android:layout_weight="0.40"
                android:id="@+id/iv_camera_button"
                android:layout_width="0dp"
                android:layout_height="60dp"
                android:layout_alignParentBottom="true"
                android:src="@drawable/shutter"
                android:text="Capture Image"
                android:visibility="visible" />

            <ImageView
                android:foreground="@drawable/white_ripple"
                android:layout_gravity="center"
                android:clickable="true"
                android:layout_weight="0.30"
                android:id="@+id/switchCamera"
                android:layout_width="0dp"
                android:layout_height="@dimen/pad30dp"
                android:layout_alignParentBottom="true"
                android:src="@drawable/rotate"
                android:text="Capture Image"
                android:visibility="visible" />






        </LinearLayout>


    </RelativeLayout>


    <TextureView

        android:foregroundGravity="center"
        android:id="@+id/texture_camera_preview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center" />

    </RelativeLayout>

就是这样!!从textureview表面获取位图,然后根据屏幕和预览分辨率对其进行裁剪。.

快乐编码..! :)

答案 1 :(得分:0)

使用下面的代码可能会解决您的问题(此代码仅将Image转换为圆形图像而不用于TextureView):

 public class ImageConverter {

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }
}