import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import java.util.HashMap;
/**
* 判断该摄像头是否具备红外模组 并返回摄像头下标
* Created by maple on 2019-12-16 09:46
* E-Mail Address:740917401@qq.com
*/
public class CameraUtils {
/**
* @param context 上下文
* @param isColor true:彩色摄像头下标 false:红外摄像头下标
* @return index:摄像头下标
*/
public static CameraBean isColorAndIrCamera(Context context, boolean isColor) {
try {
UsbManager manager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
HashMap<String, UsbDevice> usbList = manager.getDeviceList();
int index;
int colorIndex = -1;
int irIndex = -1;
for (String key : usbList.keySet()) {
UsbDevice usbDevice = usbList.get(key);
//Log.e("CameraBean", "pid:" + usbDevice.getProductId() + " vid:" + usbDevice.getVendorId() +
//" isColor:"+isColor+" "+ " getDeviceName:"+usbDevice.getDeviceName());
if (usbDevice != null) {
if (usbDevice.getProductId() == 304 && usbDevice.getVendorId() == 6257) {
String[] split = usbDevice.getDeviceName().split("/");
colorIndex = Integer.parseInt(split[split.length - 2]);
}
if (usbDevice.getProductId() == 25446
&& usbDevice.getVendorId() == 3141) {
String[] split = usbDevice.getDeviceName().split("/");
irIndex = Integer.parseInt(split[split.length - 2]);
}
}
}
//不是红外模组
if (colorIndex == -1 && irIndex == -1) {
return new CameraBean(false);
} else {
/**比较倒数第二项 作为usb端口 通电后先读取地位端口 即:001 先 004 后 所以 cameraIndex 001的是0 cameraIndex 004的是1
* 彩色摄像头 pid:304 vid:6257 isColor:true getDeviceName:/dev/bus/usb/004/002
* 红外摄像头 pid:25446 vid:3141 isColor:true getDeviceName:/dev/bus/usb/001/003
* **/
if (isColor) {//彩色摄像头的下标
index = colorIndex > irIndex ? 1 : 0;
} else {//红外摄像头的下标
index = irIndex > colorIndex ? 1: 0;
}
//Log.e("CameraBean", " index:"+index+" isColor:"+isColor);
return new CameraBean(index, true);
}
} catch (Exception e) {
Log.e("CameraUtils", e.getLocalizedMessage());
}
return new CameraBean(false);
}
public static class CameraBean {
int cameraIndex;//摄像头下标
boolean isColorAndIrCamera;//是否具备红外模组的摄像头
public CameraBean(int cameraIndex, boolean isColorAndIrCamera) {
this.cameraIndex = cameraIndex;
this.isColorAndIrCamera = isColorAndIrCamera;
}
public CameraBean(boolean isColorAndIrCamera) {
this.isColorAndIrCamera = isColorAndIrCamera;
}
public int getCameraIndex() {
return cameraIndex;
}
public boolean isColorAndIrCamera() {
return isColorAndIrCamera;
}
}
}
网友评论