Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

android - Glass camera preview display is garbled

I am trying to get a live camera preview to display in Google Glass.

I'm using all of the camera defaults (and have also tried using a few different image formats; ideally, I can use one of the YUV formats), but the image that shows up in the display is garbled, like this:

garbled preview image

The layout is simple:

<?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" />

Here is the Activity code:

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

Do you have any idea why this completely unmanipulated camera preview is not displaying correctly?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This has been answered here: https://stackoverflow.com/a/19257078/950427

Longterm:

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

Shorterm(testing):

For quick testing and usage, simply add to your outermost view:

android:layout_margin="1dp"

For example:

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...