[英]How to capture image inside the overlay in Camera2 API Android?
我只需要捕獲如圖所示的覆蓋框內的圖像。 我不需要 TextureView 中的整個圖像。 我是 Android 的新手,我無法找出提到的一些解決方案來僅捕獲矩形區域內的圖像。 疊加層必須位於中心。 我嘗試過,但我無法實現。
我跟着這個代碼 - Google Sample-Camera2Basic
要添加疊加層,我使用了以下代碼:
activity_camera2_basic_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.example.googlecamera2.AutoFitTextureView
android:id="@+id/texture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<LinearLayout
android:id="@+id/surface"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_centerInParent="true"/>
<FrameLayout
android:id="@+id/control"
android:layout_width="match_parent"
android:layout_height="112dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="@color/control_background">
<Button
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/picture" />
<ImageButton
android:id="@+id/info"
android:contentDescription="@string/description_info"
style="@android:style/Widget.Material.Light.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:padding="20dp"
android:src="@drawable/ic_action_info" />
</FrameLayout>
</RelativeLayout>
我像這樣實現了onCreateView
:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_camera2_basic, container, false);
linearLayout = view.findViewById(R.id.surface);
linearLayout.addView(new Rectangle(getActivity()));
return view;
}
矩形.java
package com.example.googlecamera2;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.View;
public class Rectangle extends View {
Paint paint = new Paint();
public Rectangle(Context context) {
super(context);
}
@Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.GREEN);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(6);
Rect rect = new Rect(120, 100, 620, 900);
canvas.drawRect(rect, paint );
}
}
請建議我如何從疊加區域中獲取裁剪后的圖像並在預覽中將疊加居中。
假設您知道相對於相機預覽區域的矩形的x,y,w,h
,即0,0,textureWidth,textureHeight
。
您還知道,當您收到 Jpeg 時,圖像的尺寸為: 0,0,jpegWidth,jpegHeight
。
現在您需要將第一個縮放到第二個:裁剪區域將是
x*jpegHeight/textureWidth
y*jpegWidth/textureHight
w*jpegHeight/textureWidth
h*jpegWidth/textureheight
寬度和高度翻轉,因為您使用縱向配置:很少有Android相機可以自己生成縱向Jpeg。 大多數情況下,他們只在 EXIF 標頭中設置旋轉標志,甚至您必須在應用程序中處理。
我initally創建的位圖,比如, capturedBitmap
。 這是相機捕獲的原始位圖。
capturedImage = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
然后我必須找到覆蓋的確切坐標。
// Set the left, top, width and height of the overlay to get that portion of the image.
// Also, rotate the image i.e., rotationMatrix
croppedImage = Bitmap.createBitmap(capturedImage, left, top, cropWidth, cropHeight, rotationMatrix, false);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.