在camera1 中zoom有几个方法:isSmoothZoomSupported,setZoom,getZoom,getMaxZoom,getZoomRatios
只有getZoomRatios这个方法的返回值是一个集合,但是看了大半天还是没看出是个啥集合。。。。下面就是我的华为手机打印出来的集合。。。。
[100, 103, 106, 109, 112, 115, 118, 121, 124, 127, 130, 133, 136, 139, 142, 145, 148, 151, 154, 157, 160, 163, 166, 169, 172, 175, 178, 181, 184, 187, 190, 193, 196, 199, 203, 206, 209, 212, 215, 218, 221, 224, 227, 230, 233, 236, 239, 242, 245, 248, 251, 254, 257, 260, 263, 266, 269, 272, 275, 278, 281, 284, 287, 290, 293, 296, 299, 303, 306, 309, 312, 315, 318, 321, 324, 327, 330, 333, 336, 339, 342, 345, 348, 351, 354, 357, 360, 363, 366, 369, 372, 375, 378, 381, 384, 387, 390, 393, 396, 399]
通过getMaxZoom得到99,与上面的数组对应,只要输入一个setZoom(index)值,就能得到相应的zoom结果。
相关代码在下面:
/**
* 获取下标,传入的参数为浮点数,就是显示的倍率
*
* @param zoomratio
* @return
*/
public int getZoomIndexByZoomratio(float zoomratio) {
List<Integer> allZoomRatios = getAllZoomRatio();
if (allZoomRatios == null) {
showErrorMessage("获取zoom集合失败");
return -3;
} else if (allZoomRatios.size() <= 0) {
showErrorMessage("获取zoom集合为空");
return -4;
}
if (zoomratio == 1.0f) {
return 0;
}
if (zoomratio == getPictureMaxZoom()) {
return allZoomRatios.size() - 1;
}
for (int i = 1; i < allZoomRatios.size(); i++) {
if (allZoomRatios.get(i) >= (zoomratio * 100) && allZoomRatios.get(i - 1) <= (zoomratio * 100)) {
return i;
}
}
return -1;
}
/**
* 获取全部zoomratio
*/
public List<Integer> getAllZoomRatio() {
Camera.Parameters parameters = getCameraParameters();
if (parameters.isZoomSupported()) {
return parameters.getZoomRatios();
} else {
return null;
}
}
/**
* 获取实际意义的最大放大倍数,如4.0,10.0
* 未完成
*
* @return
*/
public float getPictureMaxZoom() {
List<Integer> allZoomRatio = getAllZoomRatio();
if (null == allZoomRatio) {
return 1.0f;
} else {
return Math.round(allZoomRatio.get(allZoomRatio.size() - 1) / 100f);
}
}
/**
* 获取全部zoomratio
*/
public List<Integer> getAllZoomRatio() {
Camera.Parameters parameters = getCameraParameters();
if (parameters.isZoomSupported()) {
Log.e("lawwing", "getAllZoomRation = " + parameters.getZoomRatios().toString());
return parameters.getZoomRatios();
} else {
return null;
}
}
在Camera2中,又是另外一种实现方式,直接上代码:
//现在声明两个类变量 -
public float finger_spacing = 0;
public int zoom_level = 1;
//并更新给定的onTouch()方法。
public boolean onTouch(View v,MotionEvent event){
try {
Activity activity = getActivity();
CameraManager manager =(CameraManager)activity.getSystemService(Context.CAMERA_SERVICE);
CameraCharacteristics characteristics = manager.getCameraCharacteristics(mCameraId);
float maxzoom =(characteristics.get(CameraCharacteristics.SCALER_AVAILABLE_MAX_DIGITAL_ZOOM))* 10;
Rect m = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE);
int action = event.getAction();
float current_finger_spacing;
if(event.getPointerCount()> 1){
//多点触控逻辑
current_finger_spacing = getFingerSpacing(event);
if(finger_spacing!= 0){
if(current_finger_spacing> finger_spacing&& maxzoom> zoom_level){
zoom_level ++;
}否则if(current_finger_spacing< finger_spacing&& zoom_level> 1){
zoom_level--;
}
int minW =(int)(m.width()/ maxzoom);
int minH =(int)(m.height()/ maxzoom);
int difW = m.width() - minW;
int difH = m.height() - minH;
int cropW = difW / 100 *(int)zoom_level;
int cropH = difH / 100 *(int)zoom_level;
cropW - = cropW& 3;
cropH - = cropH& 3;
Rect zoom = new Rect(cropW,cropH,m.width() - cropW,m.height() - cropH);
mPreviewRequestBuilder.set(CaptureRequest.SCALER_CROP_REGION,zoom);
}
finger_spacing = current_finger_spacing;
} else {
if(action == MotionEvent.ACTION_UP){
//单触逻辑
}
}
try {
mCaptureSession
.setRepeatingRequest(mPreviewRequestBuilder.build(),mCaptureCallback,null);
} catch(CameraAccessException e){
e.printStackTrace();
} catch(NullPointerException ex){
ex.printStackTrace();
}
} catch(CameraAccessException e){
///抛出新的RuntimeException(无法访问摄像头。,e);
}
//返回true;
}
//确定前两个手指之间的空间
@SuppressWarnings(deprecation)
private float getFingerSpacing(MotionEvent event){
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return(float)Math.sqrt(x * x + y * y);
}