2015-04-22 125 views
1

我想创建和应用程序,需要我采取以前捕获的图像,并将其放置在约0.5 alpha的当前相机预览。这个“覆盖”图像可作为捕捉下一张照片的指南;客户端将使用叠加层将预览中的当前帧与先前捕获的图像进行比较。解决这个问题的一个简单方法是检索最后捕获的文件并将其设置为视图。但是,拍摄的图像并不总是与相机预览分辨率和纵横比相匹配,导致覆盖和预览在对象表示方面不一致(相同的对象在预览和捕获的图像之间尺寸可能会有所不同)。我曾尝试:安卓相机预览捕获/调整大小捕获的图像匹配预览

  • 使用相机预览回调存储当前帧的数据,这是低效和总不工作得很好(也有轻微的翻译,尺寸准确)
  • 使用上述以前文件的方法并试图按比例缩小或放大,匹配纵横比(也许我的数学是错误的[我用预览的长宽比来计算原始图像中的区域],并且仍然有可能使用这种方法?如果有人可以这将是值得赞赏的)。研究如何捕捉表面视图的内容,无济于事。

目前,有两种可能的方法,我想见解如何,我会的做法是:

  • 冻结SurfaceView以某种方式(锁定某种画布),设置不透明度和使用其他SurfaceView来显示相机(可能效率低下,笨拙的解决方法?)。
  • 当调用PictureCallback(首选)时,捕获SurfaceView的内容并存储在位图中。

当然,任何其他建议将不胜感激。

注:也有类似的问题,我花了几个小时经历他们,但他们都没有正确解决我的问题或给予足够的细节。

回答

2

我知道了使用两个TextureView部件工作:

  • 一个设定为0.5α和使用的RelativeLayout在另一个的顶部规定。
  • 有不透明TextureView注册SurfaceTextureListener
  • 开始通过onSurfaceTextureAvailable在触发相机所述收听
  • 将指定的表面纹理作为previewTexture
  • 在按下按钮,透明TextureView
  • 的画布上绘制getBitmap()

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在layout.xml:

<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" /> 
+0

谢谢!这完美地解决了我的问题。 – Shadwell