你好,游客 登录 注册 搜索
背景:
阅读新闻

Zxing 竖屏切换 Android

[日期:2014-05-02] 来源:Linux社区  作者:aaawqqq [字体:大 中 小]

Zxing 竖屏切换  Android

在google上下载的ZxingDemo 为横屏状态 

我简单的在Androidmanifest.xml把Activity换成竖屏android:screenOrientation="portrait"
 
屏幕虽然正了 但是屏幕上显示的拍摄画面还是横着的 

查了一些网上的资料  结合自己手工调试 

在这里总结分享一下
 
步骤:
 
1)  在AndroidManifest.xml中把  <Activity  />标签  CaptureActivity 的screenOrientation修改为

android:screenOrientation="portrait"

2)  在CameraManager.java类中的getFramingRectInPreview()替换掉原先的 left right top bottom

     
      //竖屏
      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;

3)  在CameraConfigurationManager.java中void setDesiredCameraParameters(Camera camera)方法
 
在setParameters之前增加

camera.setDisplayOrientation(90);

4)  在DecodeHandler.java中的 decode(byte[] data, int width, int height)方法在
 
PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(data, width, 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];
  }
  int tmp = width; // Here we are swapping, that's the difference to #11
  width = height;
  height = tmp;
  data = rotatedData;

5)  将CameraConfigurationManager的  initFromCameraParameters(Camera camera)方法整个替换如下
 
该方法防止竖屏后屏幕拉伸

 /**
  * Reads, one time, values from the camera that are needed by the app.
  */
 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);
 
    Point screenResolutionForCamera = new Point(); 
    screenResolutionForCamera.x = screenResolution.x; 
    screenResolutionForCamera.y = screenResolution.y; 
   
    // preview size is always something like 480*320, other 320*480
    if (screenResolution.x < screenResolution.y) { 
          screenResolutionForCamera.x = screenResolution.y; 
          screenResolutionForCamera.y = screenResolution.x;
    }
 
  cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);
//  cameraResolution = getCameraResolution(parameters, screenResolution);
  Log.d(TAG, "Camera resolution: " + screenResolution);
 }

至此Zxing 横屏就切换为竖屏
 
不足之处请在下方留言  谢谢
 
希望对您有用
 
资源下载地址见 http://www.linuxidc.com/Linux/2014-05/101115.htm

更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-05/101116.htm

linux
相关资讯       Android Zxing  Zxing竖屏  Android竖屏 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款