【发布时间】:2014-03-20 10:25:56
【问题描述】:
我的应用中需要两个相机预览。但是,android 相机一次只能提供一个预览。有没有办法将该预览流水线/复制到另一个视图?我遇到了这个问题How to create multi lenses or preview using one camera in Android 他这么说
On Android 3.0 or later, you can use the setPreviewTexture method to pipe the preview data into an OpenGL texture, which you can then render to multiple quads in a GLSurfaceView or equivalent.
但我不知道如何在GLSurfaceView 中将其渲染到多个四边形。我需要支持 android 4.0+。但我不想使用预览回调中的预览帧。它会导致严重的延迟。任何帮助,将不胜感激。谢谢!!
这是我的单次预览代码
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="10" >
<TextureView
android:id="@+id/textureView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5" />
<TextureView
android:id="@+id/textureView2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5" />
</LinearLayout>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity implements SurfaceTextureListener{
private Camera mCamera;
private TextureView mTextureView1;
private TextureView mTextureView2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextureView1 = (TextureView) findViewById(R.id.textureView1);
mTextureView2 = (TextureView) findViewById(R.id.textureView2);
mTextureView1.setSurfaceTextureListener(this);
}
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
int height) {
try {
mCamera = Camera.open(getCameraId());
mCamera.setPreviewTexture(surface);
CameraInfo cameraInfo = new CameraInfo();
Camera.getCameraInfo(getCameraId(), cameraInfo);
setCameraDisplayOrientation(this, getCameraId(), mCamera);
mCamera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
try {
mCamera.stopPreview();
mCamera.release();
} catch (Exception e) {
e.printStackTrace();
}
return true;
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
int height) {
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
}
输出:
【问题讨论】:
-
要明确一点:您是希望在两个相邻窗口中显示来自一个单个相机的预览帧,还是来自两个不同相机的预览帧 并排显示?
-
@fadden 我需要从两个相邻窗口中显示的单个摄像头进行预览。
-
@fadden 但正如我所说,我不想使用
onPreviewFrame来获取单个帧,因为它会引入一些延迟。 -
@AbhishekV 你完成了这个任务你能不能分享一下你是怎么做的我也卡在同一点你能帮我吗
-
@ErumHannan 我无法解决这个问题。这只是我们项目中的一个小模块,因为我忙于其他项目,所以我不能花太多时间在上面。我最终使用了 onPreviewFrame() 方法中的预览帧。也没有那么糟糕,如果您可以根据自己的需要正确设置相机预览大小,绝对不会有任何延迟。