android – 玻璃相机预览显示乱码

我正在尝试在Google Glass中显示实时相机预览.

我正在使用所有相机认设置(并且还尝试使用一些不同的图像格式;理想情况下,我可以使用其中一种YUV格式),但显示屏中显示的图像是乱码,如下所示:

布局很简单:

<?xml version="1.0" encoding="utf-8"?>
<TextureView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scan_preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

这是活动代码

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 height) {
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        }
    }
}

你知道为什么这个完全没有操作的相机预览没有正确显示吗?

解决方法

这已在这里得到解答: https://stackoverflow.com/a/19257078/950427

长期:

Camera.Parameters params = camera.getParameters();
params.setPreviewFpsRange(30000,30000);
camera.setParameters(params);

Shorterm(测试):

快速测试和使用,只需添加到最外层视图:

android:layout_margin="1dp"

例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_margin="1dp"
    android:layout_height="match_parent" >

    <org.opencv.android.JavaCameraview
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tutorial2_activity_surface_view" />

</LinearLayout>

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...