如何使用表面视图在相机的矩形部分内捕获图像?

时间:2018-11-20 09:57:46

标签: android android-camera surfaceview

我需要在相机矩形内捕获图像。 例如如下:

enter image description here

下面是xml布局

<FrameLayout 
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.view.SurfaceView
    android:id="@+id/surface"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

<View
    android:id="@+id/rectangleView"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:background="@drawable/rectangle"
    />
<Button
    android:id="@+id/button_capture"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center"
    android:text="Capture" />

所以我需要捕获矩形内部的部分。我已经尝试了以下代码段

Bitmap imageOriginal = BitmapFactory.decodeByteArray(data, 0, data.length); 
int[] customViewPosition = new int[2];
customView.getLocationInWindow(customViewPosition);
int[] surfacePosition = new int[2];
preview.getLocationInWindow(surfacePosition);
float scale = imageOriginal.getWidth() / (float) preview.getWidth();
int left = (int) ((int) scale * (customViewPosition[0] + 
customView.getWidth() / 6F - surfacePosition[0]));
int top = (int) ((int) scale * (customViewPosition[1] +   
customView.getHeight() / 6F - surfacePosition[1]));
int width = (int) scale * customView.getWidth() * 2 / 3;
int height = (int) scale * customView.getHeight() * 2 / 3;
Bitmap imageCropped = Bitmap.createBitmap(imageOriginal, 
customView.getBottom(), customView.getTop(), customView.getWidth(), 
customView.getHeight(), null, false);

以上代码对捕获图像的必需部分没有帮助。请帮忙!

0 个答案:

没有答案