Android如何用灰色阴影边框打开相机应用程序

时间:2015-09-20 10:40:46

标签: android android-camera

我需要用户使用移动相机拍照。

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

无论如何要实现这一点,任何帮助表示赞赏。 谢谢。

2 个答案:

答案 0 :(得分:0)

您需要实施自己的自定义相机才能执行此操作。您无法通过Intent从默认固件摄像头请求自定义布局。

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

答案 1 :(得分:0)

如果我想要的边框,我设法在顶部,底部,左侧和右侧使用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);