简体   繁体   English

Android相机预览捕获/调整捕获图像的大小以匹配预览

[英]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: 我努力了:

  • Using Camera Preview callbacks to store the current frame data, this is inefficient and doesnt always work well (also has a slight translation, dimensions are accurate) 使用Camera Preview回调来存储当前的帧数据,这效率低下并且并不总是能很好地工作(也有轻微的平移,尺寸是准确的)
  • Using the aforementioned previous-file method and trying to scale down or scale up accordingly, match aspect ratio (maybe my math was wrong [I used the aspect ratio of the preview to calculate the area in the original image] and it is still possible to use this approach? If someone could way in on this it would be appreciated). 使用上述以前的文件方法并尝试按比例缩小或放大,则匹配宽高比(也许我的数学是错误的[我使用预览的宽高比来计算原始图像中的面积),并且仍然有可能使用这种方法吗?如果有人可以介入,将不胜感激。
  • Researching ways to capture the contents of a surface view, to no avail. 研究无法捕获表面视图内容的方法。

Currently, two possible methods I would like insight on how I would approach would be: 目前,我想了解如何处理的两种可能方法是:

  • Freeze the SurfaceView somehow (locking the canvas of some sort), set the opacity and use another SurfaceView to display the camera (maybe inefficient, clunky workaround?). 以某种方式冻结SurfaceView(锁定某种画布),设置不透明度,并使用另一个SurfaceView显示相机(可能效率低下,笨拙的解决方法?)。
  • Capture the contents of the SurfaceView and store in a bitmap when the PictureCallback is called (preferred). 捕获(首选)PictureCallback时,捕获SurfaceView的内容并存储在位图中。

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组件使其工作:

  • One is set to 0.5 alpha and lays on top of the other using a RelativeLayout. 一个设置为0.5 alpha,并使用RelativeLayout放置在另一个之上。
  • Have the opaque TextureView register a SurfaceTextureListener 让不透明的TextureView注册一个SurfaceTextureListener
  • Start the camera triggered by onSurfaceTextureAvailable in said listener 启动由侦听器中的onSurfaceTextureAvailable触发的摄像头
  • Set the given SurfaceTexture as previewTexture 将给定的SurfaceTexture设置为PreviewTexture
  • At button press, draw getBitmap() on the canvas of the transparent TextureView 按下按钮时,在透明TextureView的画布上绘制getBitmap()

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中的T​​extureViews:

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM