使用新的android.hardware.camera2接口显示摄像头预览,可以按照以下步骤进行:
Context.getSystemService(Context.CAMERA_SERVICE)
方法获取CameraManager实例。getCameraIdList()
方法获取所有可用的摄像头ID列表。根据需求选择前置或后置摄像头。openCamera()
方法打开选定的摄像头。需要传入摄像头ID和一个CameraDevice.StateCallback回调对象,以便在摄像头打开或关闭时接收通知。createCaptureSession()
方法创建一个预览会话。需要传入一个Surface列表,其中包括用于显示预览的Surface。createCaptureRequest()
方法创建一个预览请求对象。可以设置不同的参数,如预览尺寸、自动对焦模式等。addTarget()
方法,将预览Surface作为目标添加到预览请求中。setRepeatingRequest()
方法,将预览请求设置为重复请求,即开始预览。以下是一个简单的示例代码,展示如何使用新的android.hardware.camera2接口显示摄像头预览:
import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CameraDevice;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CaptureRequest;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CAMERA_PERMISSION = 200;
private static final String TAG = "Camera2Preview";
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private CameraManager cameraManager;
private String cameraId;
private CameraDevice cameraDevice;
private CameraCaptureSession cameraCaptureSession;
private CaptureRequest.Builder captureRequestBuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
surfaceView = findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
surfaceHolder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
openCamera();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// Surface已更改,可以重新设置预览大小等参数
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
closeCamera();
}
});
}
private void openCamera() {
try {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
return;
}
cameraId = cameraManager.getCameraIdList()[0];
cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
cameraDevice = camera;
createPreviewSession();
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
camera.close();
cameraDevice = null;
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
camera.close();
cameraDevice = null;
Toast.makeText(MainActivity.this, "Camera error: " + error, Toast.LENGTH_SHORT).show();
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void createPreviewSession() {
try {
Surface surface = surfaceHolder.getSurface();
captureRequestBuilder = cameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
captureRequestBuilder.addTarget(surface);
cameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
cameraCaptureSession = session;
try {
cameraCaptureSession.setRepeatingRequest(captureRequestBuilder.build(), null, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
Toast.makeText(MainActivity.this, "Failed to configure camera session", Toast.LENGTH_SHORT).show();
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void closeCamera() {
if (cameraCaptureSession != null) {
cameraCaptureSession.close();
cameraCaptureSession = null;
}
if (cameraDevice != null) {
cameraDevice.close();
cameraDevice = null;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
Toast.makeText(this, "Camera permission denied", Toast.LENGTH_SHORT).show();
}
}
}
}
请注意,以上代码仅为示例,可能需要根据实际需求进行适当修改。同时,为了使摄像头预览正常工作,还需要在AndroidManifest.xml文件中添加相应的权限声明:
<uses-permission android:name="android.permission.CAMERA" />
希望这个示例能帮助你使用新的android.hardware.camera2接口显示摄像头预览。如果需要更多详细信息和示例代码,请参考腾讯云的相关文档和示例代码。
领取专属 10元无门槛券
手把手带您无忧上云