我正在尝试在Google Glass中显示实时相机预览.
我正在使用所有相机默认设置(并且还尝试使用一些不同的图像格式;理想情况下,我可以使用其中一种YUV格式),但显示屏中显示的图像是乱码,如下所示:
布局很简单:
这是活动代码:
public class ScanActivity extends Activity { private static final String kTag = ScanActivity.class.getSimpleName(); private TextureView mVideoCaptureView = null; private Camera mCamera = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_scan); setTitle(R.string.title_scan); mVideoCaptureView = (TextureView) findViewById(R.id.scan_preview); mVideoCaptureView.setKeepScreenOn(true); mVideoCaptureView.setSurfaceTextureListener(new VideoCaptureTextureListener()); } @Override protected void onPause() { super.onPause(); stopVideo(); } @Override protected void onResume() { super.onResume(); startVideo(); } private void startVideo() { if (mCamera != null) { mCamera.release(); } mCamera = Camera.open(); if (null != mVideoCaptureView) { try { mCamera.setPreviewTexture(mVideoCaptureView.getSurfaceTexture()); } catch (IOException e) { Log.e(kTag, "Error setting preview texture", e); return; } } mCamera.startPreview(); } private void stopVideo() { if (null == mCamera) return; try { mCamera.stopPreview(); mCamera.setPreviewDisplay(null); mCamera.setPreviewTexture(null); mCamera.release(); } catch (IOException e) { Log.w(kTag, e); } mCamera = null; } private final class VideoCaptureTextureListener implements TextureView.SurfaceTextureListener { @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { stopVideo(); return true; } @Override public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) { startVideo(); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) { } @Override public void onSurfaceTextureUpdated(SurfaceTexture surface) { } } }
你知道为什么这个完全没有操作的相机预览没有正确显示吗?