2 w530680217 w530680217 于 2016.02.25 18:26 提问

Android Camera2的使用到底是哪里错了?

图片说明
代码如下:

public class CameraTwoActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {

@Bind(R.id.textureview)
android.view.TextureView mPreviewView;
private Handler mHandler;
private HandlerThread mThreadHandler;
private Size mPreviewSize=null;
private CaptureRequest.Builder mPreviewBuilder;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera_activity);
    ButterKnife.bind(this);
    mThreadHandler = new HandlerThread("CAMERA2");
    mThreadHandler.start();
    mHandler = new Handler(mThreadHandler.getLooper());
    mPreviewView.setSurfaceTextureListener(this);
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    try {
        //获得CameraManager
        CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
        //获得属性
        CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics("0");
        //支持的STREAM CONFIGURATION
        StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
        //显示的size
        mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];
        //打开相机
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        cameraManager.openCamera("0", mCameraDeviceStateCallback, mHandler);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    return false;
}
//TextureView.SurfaceTextureListener
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
}
private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
    @Override
    public void onOpened(CameraDevice camera) {
        try {
            startPreview(camera);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onDisconnected(CameraDevice camera) {
    }
    @Override
    public void onError(CameraDevice camera, int error) {
    }
};
//开始预览,主要是camera.createCaptureSession这段代码很重要,创建会话
private void startPreview(CameraDevice camera) throws CameraAccessException {
    SurfaceTexture texture = mPreviewView.getSurfaceTexture();
    texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
    Surface surface = new Surface(texture);
    Log.i(getClass().getName(),"1111");
    try {
        mPreviewBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
    } catch (CameraAccessException e) {
        Log.i(getClass().getName(),"2222");
        e.printStackTrace();
    }
    Log.i(getClass().getName(),"3333");
    mPreviewBuilder.addTarget(surface);
    camera.createCaptureSession(Arrays.asList(surface), mSessionStateCallback, mHandler);
}
private CameraCaptureSession.StateCallback mSessionStateCallback = new CameraCaptureSession.StateCallback() {
    @Override
    public void onConfigured(CameraCaptureSession session) {
        try {
            updatePreview(session);
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onConfigureFailed(CameraCaptureSession session) {
    }
};
private void updatePreview(CameraCaptureSession session) throws CameraAccessException {
    session.setRepeatingRequest(mPreviewBuilder.build(), null, mHandler);
}

}

4个回答

u011249920
u011249920   2016.02.26 10:18

As the log said, you are trying to get width from a Null object.
texture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
mPreviewSize = null

w530680217
w530680217 开始就设置为null了。中间调用的时候是有值输出的我Log打印过。mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];执行后是有数据的。
接近 2 年之前 回复
lxk_1993
lxk_1993   Rxr 2016.02.26 11:03

空指针错误 你可以在打印的logcat信息的下面 有一个 你的类的代码行 提示 会 定位到 哪一行的

w530680217
w530680217 所有的错误都在这里,没有提示定位的绿色类。全是系统的一些类
接近 2 年之前 回复
oQingShuYe
oQingShuYe   2016.03.01 08:29

楼主,问题解决了没?这个camera2我也不会用。
你的代码里有一句。mThreadHandler = new HandlerThread("CAMERA2");
我看到SDK->Samples->media->Camera2Basic里用的是 new HandlerThread("CameraBackground")

liyuanjinglyj
liyuanjinglyj 回复青树叶: 这就是起个名字,是一样的。
接近 2 年之前 回复
enjoyGai
enjoyGai   2017.02.16 11:10

建议去下gogle的demo,改改,省事,不要自己写!还有你这明显是空指针了,debug调一下吧,

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!