目前Camera API 1使用的比较少了,一般代码如下。遇到的问题是camera1接口无法打开相机,cam2接口打开正常。最后排查结果是 sensor的 frameRate 配置不对。
Camera camera;
CameraPreview mPreview;
@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cam_1_layout);
mPreview = findViewById(R.id.camera_1_preview);
try {
camera = Camera.open(0);
mPreview.setCamera(camera);
} catch (Exception e) {
e.printStackTrace();
}
}
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private Camera mCamera;
public CameraPreview(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CameraPreview(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);