Android 如何打开带有灰色阴影边框的相机应用

Android how to open camera app with grey shaded borders

我需要用户使用手机相机拍照。

我需要相机有阴影灰色边框,这样只有框内的部分会被裁剪和拍摄,图像的其余部分应该被忽略。

有没有办法实现这个,感谢任何帮助。 谢谢。

您需要实现自己的自定义相机才能执行此操作。您无法通过 Intent.

从默认固件相机请求自定义布局

有关详细信息,请参阅 Android 开发人员文档:http://developer.android.com/guide/topics/media/camera.html#custom-camera

我设法通过在顶部、底部、左侧和右侧使用 4 个阴影灰色边框(如果我想要的边框)来做到这一点。

 <SurfaceView
    android:id="@+id/cameraPreview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    />


<View
    android:id="@+id/rectangleView"
    android:layout_height="150dp"
    android:layout_width="280dp"
    android:layout_centerInParent="true"
    android:background="@drawable/rectangle_shape"
    />

<!--I need only the part of the Image inside this view-->
<View
    android:id="@+id/viewLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#88000000"
    android:layout_toLeftOf="@+id/rectangleView"
    />

<View
    android:id="@+id/viewRight"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#88000000"
    android:layout_toRightOf="@+id/rectangleView"
    />

<View
    android:id="@+id/viewUp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#88000000"
    android:layout_above="@+id/rectangleView"
    android:layout_toLeftOf="@+id/viewRight"
    android:layout_toRightOf="@+id/viewLeft"
    />

<View
    android:id="@+id/viewDown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#88000000"
    android:layout_below="@+id/rectangleView"
    android:layout_toLeftOf="@+id/viewRight"
    android:layout_toRightOf="@+id/viewLeft"
    />

并获取矩形内的照片片段:

//photoByteArray is the ByteArray that holds the picture, it is retrieved from PictureCallback
Bitmap bitmap = BitmapFactory.decodeByteArray(photoByteArray, 0, photoByteArray.length);

//because the photo is always returned in landscape orientation
//we use the Matrix to rotate it
//disregard the Matrix if u need the photo in landscape
Matrix matrix = new Matrix();
matrix.postRotate(90);

//creating the bitmap that holds the photo
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, false);

//scaling the bitmap for the layout width and height.
//layout is the layout that holds the Camera SurfaceView
bitmap = Bitmap.createScaledBitmap(bitmap, layoutWidth, layoutHeight, false);

//get the desired View's Co-ordinates
int x1 = rectView.getLeft();
int y1 = rectView.getTop();
int x2 = rectView.getRight();
int y2 = rectView.getBottom();

//Create new Bitmap that contains the part inside the rectangle only
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, x1, y1, x2-x1, y2-y1);