本讲是android camera专题系列的第6讲,我们介绍android camera2 api专题的open、close camera代码实现部分。
更多资源:
资源 | 描述 |
---|---|
在线课程 | |
知识星球 | 星球名称:深入浅出android camera 星球id: 17296815 |
极客笔记圈 |
枚举camera数量
public int getnumberofcameras() {
try {
string[] cameraidarray = mcameramanager.getcameraidlist();
if(mydebug.log) {
log.d(tag, "getcameraidlist length:" cameraidarray.length);
}
for (int i = 0; i < cameraidarray.length; i ) {
islogicalmulticamera(mcontext, i);
if(mydebug.log) {
log.d(tag, "cameraid:" i ", facing:" getfacing(i));
}
}
return cameraidarray.length;
} catch(throwable e) {
if(mydebug.log) {
log.e(tag, "exception trying to get camera ids");
}
e.printstacktrace();
}
return 0;
}
判断camera facing
@override
public cameracontroller.facing getfacing(int cameraid) {
try {
string cameraids = mcameramanager.getcameraidlist()[cameraid];
cameracharacteristics characteristics = mcameramanager.getcameracharacteristics(cameraids);
switch(characteristics.get(cameracharacteristics.lens_facing)) {
case camerametadata.lens_facing_front:
return cameracontroller.facing.facing_front;
case camerametadata.lens_facing_back:
return cameracontroller.facing.facing_back;
case camerametadata.lens_facing_external:
return cameracontroller.facing.facing_external;
}
log.e(tag, "unknown camera_facing: " characteristics.get(cameracharacteristics.lens_facing));
} catch(throwable e) {
if(mydebug.log)
log.e(tag, "exception trying to get camera characteristics");
e.printstacktrace();
}
return cameracontroller.facing.facing_unknown;
}
判断是否是logical multicamera
@override
public boolean islogicalmulticamera(context context, int cameraid) {
try {
string cameraids = mcameramanager.getcameraidlist()[cameraid];
cameracharacteristics characteristics = mcameramanager.getcameracharacteristics(cameraids);
set physicalcameraids = characteristics.getphysicalcameraids();
int[] capabilities = characteristics.get(cameracharacteristics.request_available_capabilities);
list capabilitieslist = new arraylist<>();
for(integer capability : capabilities) {
capabilitieslist.add(capability);
}
int logicalmulticameraid = cameracharacteristics.request_available_capabilities_logical_multi_camera;
log.d(tag, "(logicalcamera: " cameraids
",physicalcameraids:" physicalcameraids.tostring()
",has logical_multi_camera capability:"
capabilitieslist.contains(logicalmulticameraid) ")");
return capabilitieslist.contains(logicalmulticameraid) && (capabilities.length > 0);
} catch (throwable e) {
if(mydebug.log)
log.e(tag, "exception trying to islogicalmulticamera.");
e.printstacktrace();
}
return false;
}
opencamera流程代码实现
实战代码流程
preview#onsurfacetextureavailable
preview#mysurfacecreated
preview#opencamera
preview#mopencameratask.execute();
preview#opencameracore
new cameracontroller2
manager.opencamera
close camera代码实现
实战代码流程
preview#onsurfacetexturedestroyed
preview#mysurfacedestroyed
preview#closecamera
new closecameratask
cameracontroller2#release
mcameradevice.close