gpt4 book ai didi

android - 使用表面纹理的相机预览

转载 作者:太空宇宙 更新时间:2023-11-03 12:52:41 26 4
gpt4 key购买 nike

我正在尝试使用 SurfaceTexture 从我的设备的后置摄像头显示摄像头预览,但我一直收到错误

bindTextureImage : error binding external texture image 0x2:0x502

看那一行

SurfaceTexture.UpdateTexImage()

查看 android 文档,似乎这可能是由于在 SurfaceTexture.OnFrameAvailableListener 的 OnFrameAvailable 方法上调用 UpdateTexImage 引起的。根据文档“回调 (SurfaceTexture.OnFrameAvailableListener) 可能在任意线程上调用,因此在不首先将 OpenGL ES 上下文绑定(bind)到调用回调的线程的情况下调用 updateTexImage() 是不安全的。

如何将 OpenGL ES 上下文“绑定(bind)”到调用回调的线程?

我一直在努力密切关注 Grafika 中的“相机纹理” Activity 尝试获取有关如何完成此任务的线索。感谢任何帮助。

我的完整代码粘贴在下面:

public class CameraPreviewFromTextureActivity extends Activity 
{
private static String TAG = "CameraPreviewFromTexture";

private static SurfaceHolder mySurfaceHolder = null;
private SurfaceTexture mCameraTexture = null;
private Camera mCamera = null;
private EglCore mEglCore;
private WindowSurface mWindowSurface;
private int mWindowSurfaceWidth;
private int mWindowSurfaceHeight;
private int mCameraPreviewWidth, mCameraPreviewHeight;
private float mCameraPreviewFps;
private Texture2dProgram mTexProgram;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_preview_from_texture);

mySurfaceHolder = ((SurfaceView)this.findViewById(R.id.surfaceView)).getHolder();
mySurfaceHolder.addCallback(mySurfaceHolderCallback);

//Run Thread Methods
mEglCore = new EglCore(null, 0);
openCamera(328, 288, 30);
}

private SurfaceHolder.Callback mySurfaceHolderCallback = new SurfaceHolder.Callback()
{

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
releaseGl();
}

@Override
public void surfaceCreated(SurfaceHolder holder)
{
surfaceAvailable(holder, true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
Log.d(TAG, "SurfaceChanged " + width + "x" + height);

mWindowSurfaceWidth = width;
mWindowSurfaceHeight = height;
finishSurfaceSetup();
}
};


private void surfaceAvailable(SurfaceHolder holder, boolean newSurface)
{
Surface surface = holder.getSurface();
mWindowSurface = new WindowSurface(mEglCore, surface, false);
mWindowSurface.makeCurrent();

// Create and configure the SurfaceTexture, which will receive frames from the
// camera. We set the textured rect's program to render from it.
mTexProgram = new Texture2dProgram(Texture2dProgram.ProgramType.TEXTURE_EXT);
int textureId = mTexProgram.createTextureObject();
mCameraTexture = new SurfaceTexture(textureId);
//mRect.setTexture(textureId);

if (!newSurface) {
// This Surface was established on a previous run, so no surfaceChanged()
// message is forthcoming. Finish the surface setup now.
//
// We could also just call this unconditionally, and perhaps do an unnecessary
// bit of reallocating if a surface-changed message arrives.
mWindowSurfaceWidth = mWindowSurface.getWidth();
mWindowSurfaceHeight = mWindowSurface.getHeight();
finishSurfaceSetup();
}

mCameraTexture.setOnFrameAvailableListener(myOnFrameListner);
}

private SurfaceTexture.OnFrameAvailableListener myOnFrameListner = new SurfaceTexture.OnFrameAvailableListener()
{
@Override
public void onFrameAvailable(SurfaceTexture surfaceTexture)
{
surfaceTexture.updateTexImage(); //Problem Occurs Here
draw();
}
};

private void draw()
{
GlUtil.checkGlError("draw start");

GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
//mRect.draw(mTexProgram, mDisplayProjectionMatrix);
mWindowSurface.swapBuffers();

GlUtil.checkGlError("draw done");
}
private void finishSurfaceSetup()
{
int width = mWindowSurfaceWidth;
int height = mWindowSurfaceHeight;

// Use full window.
GLES20.glViewport(0, 0, width, height);

// Ready to go, start the camera.
Log.d(TAG, "starting camera preview");
try {
mCamera.setPreviewTexture(mCameraTexture);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
mCamera.startPreview();
}

private void openCamera(int desiredWidth, int desiredHeight, int desiredFps)
{
if (mCamera != null)
{
throw new RuntimeException("camera already initialized");
}

Camera.CameraInfo info = new Camera.CameraInfo();

try
{
mCamera = Camera.open();
}
catch(Exception ex)
{
ex.printStackTrace();
}

Camera.Parameters parms = mCamera.getParameters();

// Try to set the frame rate to a constant value.
int thousandFps = chooseFixedPreviewFps(parms, desiredFps * 1000);

// Give the camera a hint that we're recording video. This can have a big
// impact on frame rate.
parms.setRecordingHint(true);

mCamera.setParameters(parms);

int[] fpsRange = new int[2];
Camera.Size mCameraPreviewSize = parms.getPreviewSize();
parms.getPreviewFpsRange(fpsRange);
String previewFacts = mCameraPreviewSize.width + "x" + mCameraPreviewSize.height;
if (fpsRange[0] == fpsRange[1]) {
previewFacts += " @" + (fpsRange[0] / 1000.0) + "fps";
} else {
previewFacts += " @[" + (fpsRange[0] / 1000.0) +
" - " + (fpsRange[1] / 1000.0) + "] fps";
}
Log.i(TAG, "Camera config: " + previewFacts);

mCameraPreviewWidth = mCameraPreviewSize.width;
mCameraPreviewHeight = mCameraPreviewSize.height;
mCameraPreviewFps = desiredFps;
}


public static int chooseFixedPreviewFps(Camera.Parameters parms, int desiredThousandFps)
{
List<int[]> supported = parms.getSupportedPreviewFpsRange();

for (int[] entry : supported) {
//Log.d(TAG, "entry: " + entry[0] + " - " + entry[1]);
if ((entry[0] == entry[1]) && (entry[0] == desiredThousandFps)) {
parms.setPreviewFpsRange(entry[0], entry[1]);
return entry[0];
}
}

int[] tmp = new int[2];
parms.getPreviewFpsRange(tmp);
int guess;
if (tmp[0] == tmp[1]) {
guess = tmp[0];
} else {
guess = tmp[1] / 2; // shrug
}

Log.d(TAG, "Couldn't find match for " + desiredThousandFps + ", using " + guess);
return guess;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.camera_preview_from_texture, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

private void releaseGl() {
GlUtil.checkGlError("releaseGl start");

if (mWindowSurface != null) {
mWindowSurface.release();
mWindowSurface = null;
}
if (mTexProgram != null) {
mTexProgram.release();
mTexProgram = null;
}
GlUtil.checkGlError("releaseGl done");

mEglCore.makeNothingCurrent();
}

private void releaseCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
Log.d(TAG, "releaseCamera -- done");
}
}

@Override
public void onDestroy()
{
releaseCamera();
releaseGl();
mEglCore.release();
}

}

最佳答案

每个线程都有一个“当前”EGL 上下文,由 GLES 驱动程序通过线程本地存储引用。您发出的任何 GLES 命令,包括纹理绑定(bind),都在此上下文中运行。

快速浏览一下您的代码表明您正在尝试在 UI 线程上执行所有操作。如果您创建自己的上下文并使其成为当前上下文,它将与 UI 代码发生冲突,UI 代码将拥有自己的 EGL 上下文以进行硬件加速 View 渲染。如果您创建一个单独的线程,使您的 EGL 上下文处于当前状态,然后就不管它了,那么生活会简单一些。

您可以看到“来自相机的纹理” Activity 的帧可用处理程序只是:

    public void onFrameAvailable(SurfaceTexture surfaceTexture) {
mHandler.sendFrameAvailable();
}

因为帧可用消息可以到达任意线程,所以您不知道那里当前的 EGL 上下文是什么(或者线程是否有)。您可以调用 eglMakeCurrent()(通过 EglCore#makeCurrent())使您的线程成为当前线程,但您不能从多个线程使用相同的 EGL 上下文,所以如果它同时在其他地方出现,你可能会遇到问题。因此,将帧可用消息转发到您知道 EGL 上下文是最新的线程会更容易。

FWIW,0x0502 是 GL_INVALID_OPERATION

关于android - 使用表面纹理的相机预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27626002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com