[英]Android camera preview capture/resizing captured image to match preview
I am trying to create and application that requires me to take the previously captured image and place it over the current camera preview with about 0.5 alpha. 我正在尝试创建和应用程序,要求我拍摄以前捕获的图像,并将其放置在当前相机预览中(约0.5 alpha)。 This "overlay" image serves as a guide for capturing the next picture;
此“重叠”图像用作捕获下一张图片的指南; the client will use the overlay to compare the current frame in the preview to the previously captured image.
客户将使用叠加层将预览中的当前帧与先前捕获的图像进行比较。 A simple way to approach this was to retrieve the last file that was captured and set that to a view.
一种简单的方法是检索最后捕获的文件并将其设置为视图。 However, the captured image does not always match the camera previews resolution and aspect ratio, causing the overlay and preview to mismatch in representation of objects (the same object can vary in size between the preview and the captured image).
但是,捕获的图像并不总是与相机预览的分辨率和宽高比匹配,从而导致覆盖图和预览的对象表示不匹配(同一对象在预览图像和捕获的图像之间可能会有所不同)。 I have tried:
我努力了:
Currently, two possible methods I would like insight on how I would approach would be: 目前,我想了解如何处理的两种可能方法是:
Ofcourse, any other suggestions would be greatly appreciated. 当然,任何其他建议将不胜感激。
NOTE: There are similar questions and I've spent hours going through them but none of them correctly address my issue or give enough detail. 注意:还有类似的问题,我花了数小时来研究它们,但是没有一个能正确解决我的问题或提供足够的细节。
I got it to work using two TextureView components: 我使用两个TextureView组件使其工作:
Activity.java (implementing SurfaceTextureListener): Activity.java(实现SurfaceTextureListener):
@Override
public void onClick(View view) {
{
Canvas canvas = alphaCameraTextureView.lockCanvas();
canvas.drawBitmap( cameraTextureView.getBitmap(), 0, 0, null );
alphaCameraTextureView.unlockCanvasAndPost( canvas );
}
@Override
public void onSurfaceTextureAvailable( SurfaceTexture surfaceTexture, int width, int height )
{
try
{
camera = Camera.open();
camera.setDisplayOrientation( 90 );
camera.setPreviewTexture( surfaceTexture );
camera.startPreview();
}
catch (IOException e ) { e.printStackTrace(); }
}
@Override
public boolean onSurfaceTextureDestroyed( SurfaceTexture surfaceTexture )
{
camera.stopPreview();
camera.release();
return false;
}
TextureViews in the layout.xml: layout.xml中的TextureViews:
<TextureView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cameraTextureView"
android:layout_weight="0"/>
<TextureView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/alphaCameraTextureView"
android:alpha="0.5"
android:layout_weight="0" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.