本文主要是介绍android Zxing二维码扫描的两个问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Zxing二维码扫描,Android中的扫描二维码库,官方提公的库,缺少两个小功能:
1、手指放大
2、仿微信,自动放大
解决办法(参考大神博客,做一下记录,直接集成使用):
1、手指放大 (https://blog.csdn.net/qq_24531461/article/details/62887658)
2、自动放大 (https://blog.csdn.net/u010705554/article/details/78204090?locationNum=9&fps=1)
下面是我集成的部分代码
1、手指放大:
CaptureActivity中添加代码:
第一步:
autoScannerView.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if(cameraManager == null){return false;}if (event.getPointerCount() == 1) {handleFocusMetering(event, cameraManager.getCamera());} else {switch (event.getAction() & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_POINTER_DOWN:oldDist = getFingerSpacing(event);break;case MotionEvent.ACTION_MOVE:float newDist = getFingerSpacing(event);if (newDist > oldDist) {Log.e("Camera","进入放大手势");handleZoom(true, cameraManager.getCamera());} else if (newDist < oldDist) {Log.e("Camera","进入缩小手势");handleZoom(false, cameraManager.getCamera());}oldDist = newDist;break;}}return true;}
});
第二步:
cameraManager.getCamera() 这个需要在 CameraManager 里面添加一个方法:
public synchronized Camera getCamera(){if(camera!=null){return camera.getCamera();}return null;
}
第三步:
/**********************************
*** 手势放大 (直接拷贝集成到CaptureActivity里面)
*******************************************/private void handleZoom(boolean isZoomIn, Camera camera) {if(camera==null)return;Log.e("Camera","进入缩小放大方法");Camera.Parameters params = camera.getParameters();if (params.isZoomSupported()) {int maxZoom = params.getMaxZoom();int zoom = params.getZoom();if (isZoomIn && zoom < maxZoom) {Log.e("Camera","进入放大方法zoom="+zoom);zoom++;} else if (zoom > 0) {Log.e("Camera","进入缩小方法zoom="+zoom);zoom--;}if(zoom > 30){return;}params.setZoom(zoom);camera.setParameters(params);} else {Log.i("TAG", "zoom not supported");}}private static void handleFocusMetering(MotionEvent event, Camera camera) {if(camera==null)return;Log.e("Camera","进入handleFocusMetering");Camera.Parameters params = camera.getParameters();Camera.Size previewSize = params.getPreviewSize();Rect focusRect = calculateTapArea(event.getX(), event.getY(), 1f, previewSize);Rect meteringRect = calculateTapArea(event.getX(), event.getY(), 1.5f, previewSize);camera.cancelAutoFocus();if (params.getMaxNumFocusAreas() > 0) {List<Camera.Area> focusAreas = new ArrayList<>();focusAreas.add(new Camera.Area(focusRect, 800));params.setFocusAreas(focusAreas);} else {Log.i("TAG", "focus areas not supported");}if (params.getMaxNumMeteringAreas() > 0) {List<Camera.Area> meteringAreas = new ArrayList<>();meteringAreas.add(new Camera.Area(meteringRect, 800));params.setMeteringAreas(meteringAreas);} else {Log.i("TAG", "metering areas not supported");}final String currentFocusMode = params.getFocusMode();params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);camera.setParameters(params);camera.autoFocus(new Camera.AutoFocusCallback() {@Overridepublic void onAutoFocus(boolean success, Camera camera) {Camera.Parameters params = camera.getParameters();params.setFocusMode(currentFocusMode);camera.setParameters(params);}});}private static float getFingerSpacing(MotionEvent event) {float x = event.getX(0) - event.getX(1);float y = event.getY(0) - event.getY(1);Log.e("Camera","getFingerSpacing ,计算距离 = " + (float) Math.sqrt(x * x + y * y));return (float) Math.sqrt(x * x + y * y);}private static Rect calculateTapArea(float x, float y, float coefficient, Camera.Size previewSize) {float focusAreaSize = 300;int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue();int centerX = (int) (x / previewSize.width - 1000);int centerY = (int) (y / previewSize.height - 1000);int left = clamp(centerX - areaSize / 2, -1000, 1000);int top = clamp(centerY - areaSize / 2, -1000, 1000);RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right), Math.round(rectF.bottom));}private static int clamp(int x, int min, int max) {if (x > max) {return max;}if (x < min) {return min;}return x;}
/**********************************手势放大********************************************/
2、仿微信,扫描自动放大:
https://blog.csdn.net/u010705554/article/details/78204090?locationNum=9&fps=1 这博客里已经很详细了,不做重复了,自己查看吧
这篇关于android Zxing二维码扫描的两个问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!