ZXing修改为竖屏的具体操作方法

现在网上的修改方法很多,但是好像都跟我下载的源码对不起来,我按这里的源码对我下载的版本进行了修改

1.首先需要添加权限:

 
    
    
    

2.增加activity


这里的
android:screenOrientation控制了扫描横屏还是竖屏  横屏landscape

3.修改DecodeHandler 
增加一个方法
public byte[] rotateData(byte[] data,int width,int height){
		//  //旋转数据
		  byte[] rotatedData = new byte[data.length];
		  for (int y = 0; y < height; y++) {
		      for (int x = 0; x < width; x++)
		          rotatedData[x * height + height - y - 1] = data[x + y * width];
		  }
		    return rotatedData;
	  }
对decode(byte[] data, int width, int height)方法进行修改
 PlanarYUVLuminanceSource source = activity.getCameraManager().buildLuminanceSource(rotateData(data, width, height), width, height);
4.修改CameraConfigurationManager
因为这里需要修改的地方比较多,我把整个代码拿出来吧
修改initFromCameraParameters
void initFromCameraParameters(Camera camera) {
	  Camera.Parameters parameters = camera.getParameters();
	    previewFormat = parameters.getPreviewFormat();
	    previewFormatString = parameters.get("preview-format");
	    Log.d(TAG, "Default preview format: " + previewFormat + '/' + previewFormatString);
	    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
	    Display display = manager.getDefaultDisplay();
	    screenResolution = new Point(display.getWidth(), display.getHeight());
	    Log.d(TAG, "Screen resolution: " + screenResolution);
	    cameraResolution = getCameraResolution(parameters, screenResolution);
	    Log.d(TAG, "Camera resolution: " + screenResolution);
	  }
增加
private int previewFormat;
	  private String previewFormatString;
  private static final Pattern COMMA_PATTERN = Pattern.compile(",");
 private static Point getCameraResolution(Camera.Parameters parameters, Point screenResolution) {


	    String previewSizeValueString = parameters.get("preview-size-values");
	    // saw this on Xperia
	    if (previewSizeValueString == null) {
	      previewSizeValueString = parameters.get("preview-size-value");
	    }


	    Point cameraResolution = null;


	    if (previewSizeValueString != null) {
	      Log.d(TAG, "preview-size-values parameter: " + previewSizeValueString);
	      cameraResolution = findBestPreviewSizeValue(previewSizeValueString, screenResolution);
	    }


	    if (cameraResolution == null) {
	      // Ensure that the camera resolution is a multiple of 8, as the screen may not be.
	      cameraResolution = new Point(
	          (screenResolution.x >> 3) << 3,
	          (screenResolution.y >> 3) << 3);
	    }


	    return cameraResolution;
	  }
  private static Point findBestPreviewSizeValue(CharSequence previewSizeValueString, Point screenResolution) {
	    int bestX = 0;
	    int bestY = 0;
	    int diff = Integer.MAX_VALUE;
	    for (String previewSize : COMMA_PATTERN.split(previewSizeValueString)) {


	      previewSize = previewSize.trim();
	      int dimPosition = previewSize.indexOf('x');
	      if (dimPosition < 0) {
	        Log.w(TAG, "Bad preview-size: " + previewSize);
	        continue;
	      }


	      int newX;
	      int newY;
	      try {
	        newX = Integer.parseInt(previewSize.substring(0, dimPosition));
	        newY = Integer.parseInt(previewSize.substring(dimPosition + 1));
	      } catch (NumberFormatException nfe) {
	        Log.w(TAG, "Bad preview-size: " + previewSize);
	        continue;
	      }


	      int newDiff = Math.abs(newX - screenResolution.x) + Math.abs(newY - screenResolution.y);
	      if (newDiff == 0) {
	        bestX = newX;
	        bestY = newY;
	        break;
	      } else if (newDiff < diff) {
	        bestX = newX;
	        bestY = newY;
	        diff = newDiff;
	      }


	    }


	    if (bestX > 0 && bestY > 0) {
	      return new Point(bestX, bestY);
	    }
	    return null;
	  }

setDesiredCameraParameters方法里增加camera.setDisplayOrientation(90); //此句为转为横屏时增加的

这样ZXing就可以竖屏扫描了,但是主要还有一个问题改了之后只能扫条形码不能扫二维码。这个以后还得研究

CameraManager的修改这里没用到 ,还是记录一下吧
getFramingRectInPreview方法
   //横屏
      rect.left = rect.left * cameraResolution.x / screenResolution.x;
      rect.right = rect.right * cameraResolution.x / screenResolution.x;
      rect.top = rect.top * cameraResolution.y / screenResolution.y;
      rect.bottom = rect.bottom * cameraResolution.y / screenResolution.y;
      //竖屏
//      rect.left = rect.left * cameraResolution.y / screenResolution.x; 
//      rect.right = rect.right * cameraResolution.y / screenResolution.x; 
//      rect.top = rect.top * cameraResolution.x / screenResolution.y; 
//      rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y; 


你可能感兴趣的:(开源项目研究)