android usb camera 例子
时间: 2023-07-29 22:09:16 浏览: 198
Android系统 USBCamera使用案例
5星 · 资源好评率100%
以下是一个示例代码,用于在Android设备上使用USB摄像头:
```
public class MainActivity extends AppCompatActivity {
private final String TAG = "MainActivity";
private final int VID = 0x1a86;
private final int PID = 0x7523;
private final int REQUEST_CAMERA_PERMISSION = 0x01;
private UsbManager mUsbManager;
private CameraManager mCameraManager;
private String[] mCameraIds;
private CameraDevice mCameraDevice;
private CameraCaptureSession mCaptureSession;
private SurfaceTexture mSurfaceTexture;
private Size mPreviewSize;
private HandlerThread mBackgroundThread;
private Handler mBackgroundHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mUsbManager = (UsbManager) getSystemService(Context.USB_SERVICE);
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
} else {
startCamera();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CAMERA_PERMISSION) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
startCamera();
} else {
Toast.makeText(this, "请允许访问相机权限!", Toast.LENGTH_SHORT).show();
}
}
}
private void startCamera() {
try {
mCameraIds = mCameraManager.getCameraIdList();
for (String cameraId : mCameraIds) {
CameraCharacteristics characteristics = mCameraManager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != null && facing == CameraCharacteristics.LENS_FACING_EXTERNAL) {
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
if (map != null) {
mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0];
Log.d(TAG, "Preview size: " + mPreviewSize);
UsbDevice usbDevice = null;
for (UsbDevice device : mUsbManager.getDeviceList().values()) {
if (device.getVendorId() == VID && device.getProductId() == PID) {
usbDevice = device;
break;
}
}
if (usbDevice != null) {
mUsbManager.requestPermission(usbDevice, new UsbPermissionReceiver() {
@Override
public void onPermissionGranted(UsbDevice device) {
try {
mCameraManager.openCamera(cameraId, new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
mCameraDevice = camera;
startPreview();
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
camera.close();
mCameraDevice = null;
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
camera.close();
mCameraDevice = null;
}
}, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onPermissionDenied(UsbDevice device) {
Toast.makeText(MainActivity.this, "USB相机权限被拒绝!", Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(this, "未找到USB相机!", Toast.LENGTH_SHORT).show();
}
}
}
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void startPreview() {
try {
mSurfaceTexture = new SurfaceTexture(0);
mSurfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
Surface surface = new Surface(mSurfaceTexture);
mCameraDevice.createCaptureSession(Arrays.asList(surface), new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
try {
mCaptureSession = session;
CaptureRequest.Builder builder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW);
builder.addTarget(surface);
CaptureRequest request = builder.build();
mCaptureSession.setRepeatingRequest(request, null, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
Toast.makeText(MainActivity.this, "相机预览配置失败!", Toast.LENGTH_SHORT).show();
}
}, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
if (mBackgroundThread == null) {
mBackgroundThread = new HandlerThread("CameraBackground");
mBackgroundThread.start();
mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
}
}
@Override
protected void onPause() {
super.onPause();
if (mCameraDevice != null) {
mCameraDevice.close();
mCameraDevice = null;
}
if (mCaptureSession != null) {
mCaptureSession.close();
mCaptureSession = null;
}
if (mSurfaceTexture != null) {
mSurfaceTexture.release();
mSurfaceTexture = null;
}
if (mBackgroundThread != null) {
mBackgroundThread.quitSafely();
try {
mBackgroundThread.join();
mBackgroundThread = null;
mBackgroundHandler = null;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
```
请注意,此示例代码仅用于演示如何在Android设备上使用USB摄像头。您需要根据自己的需求进行修改和调整。
阅读全文