Android Zxing 扫描条码实现竖屏模式(portrait mode) 摄像头camera 旋转90度

最近在搞一个关于条形码扫描的软件,需求需要扫描时是竖屏。
最后在zxing官方wiki上面找到解决办法。基本思路如下。
There are 4 relative files:
1, manifest.xml, you need to make CaptureActivity portrait.

2, DecodeHandler.java, rotate data before buildLuminanceSource, it works becuase in YCbCr_420_SP and YCbCr_422_SP, the Y channel is planar and appears first

1byte[] rotatedData = new byte[data.length];
2for (int y = 0; y < height; y++) {
3    for (int x = 0; x < width; x++)
4        rotatedData[x * height + height - y - 1] = data[x + y * width];
5}

3, CameraManager.java, getFramingRectInPreview() need to be modified.

1rect.left = rect.left * cameraResolution.y / screenResolution.x;
2rect.right = rect.right * cameraResolution.y / screenResolution.x;
3rect.top = rect.top * cameraResolution.x / screenResolution.y;
4rect.bottom = rect.bottom * cameraResolution.x / screenResolution.y;

4, CameraConfigurationManager.java, set camera orientation to portrait in setDesiredCameraParameters() use

1parameters.set("orientation""portrait");

注:版本兼容请看下面。
and in getCameraResolution(), you need to swap x and y, because camera preview size is something like 480*320, other than 320*480.

1int tmp = cameraResolution.x;
2cameraResolution.x = cameraResolution.y;
3cameraResolution.y = tmp;
4return cameraResolution;

说明:
关于摄像头旋转90度的时候,不同的sdk版本方法不同。
兼容方法如下

01if (Integer.parseInt(Build.VERSION.SDK) >= <img src="http://www.andcoder.com/wp-includes/images/smilies/icon_cool.gif" alt="8)" class="wp-smiley">
02   setDisplayOrientation(camera, 90);
03  else {
04   if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
05    parameters.set("orientation""portrait");
06    parameters.set("rotation"90);
07   }
08   if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
09    parameters.set("orientation""landscape");
10    parameters.set("rotation"90);
11   }
12}
13 
14protected void setDisplayOrientation(Camera camera, int angle) {
15  Method downPolymorphic;
16  try {
17   downPolymorphic = camera.getClass().getMethod(
18     "setDisplayOrientation"new Class[] { int.class });
19   if (downPolymorphic != null)
20    downPolymorphic.invoke(camera, new Object[] { angle });
21  catch (Exception e1) {
22  }
23 }
<!--EndFragment-->
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值