Android表面无效

时间:2017-02-13 21:23:13

标签: android surfaceview

我在android系统上开发了一个活动,我在运行时遇到了一个问题:当启动活动时,我收到日志“D / Camera:app传递NULL表面”并且在表面视图上没有显示相机的图像。我在代码中插入了对SurfaceHolder对象的'isValid'函数的检查,函数返回false。

这是活动的代码。

package marl.com.mbpspay;

import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback {

    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;

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

        surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        try{
            camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
        }catch(RuntimeException e){
            Log.e("CameraActivity", "init_camera: " + e);
            return;
        }

        if(surfaceHolder.getSurface().isValid()) {
            Log.i("PreviewCallback", "Camera open [" + surfaceHolder + "]");
        }
        Camera.Parameters param;
        param = camera.getParameters();
        //modify parameter
        param.setPreviewFrameRate(20);
        param.setPreviewSize(176, 144);
        camera.setParameters(param);

        Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
            @Override
            public void onPreviewFrame(byte[] data, Camera camera) {
                Log.i("PreviewCallback","image get");
            }
        };

        try {
            Log.i("PreviewCallback","STEP 0 ["+surfaceHolder.getSurface()+"]");
            camera.setPreviewDisplay(surfaceHolder);
            Log.i("PreviewCallback","STEP 1 ["+surfaceHolder+"]");
            camera.setPreviewCallback(previewCallback);
            Log.i("PreviewCallback","STEP 2 ["+surfaceHolder+"]");
            camera.startPreview();
            //camera.takePicture(shutter, raw, jpeg)
        } catch (Exception e) {
            Log.e("CameraActivity", "init_camera: " + e);
            return;
        }
    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

}

这里是布局的xml。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_camera"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="marl.com.mbpspay.CameraActivity">

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/surfaceView" />
</RelativeLayout>

有人可以找到解决此问题的方法吗?

1 个答案:

答案 0 :(得分:0)

看起来您正在使用Surface,无论它是否是#Valid&#34;或者不是,先发制人地startPreview()。由于您使用的是onCreate()方法,因此表面可能尚未创建。在尝试开始surfaceCreated()预览之前,您应该等待SurfaceHolder.Callback Camera回调。