基于zxing实现二维码竖屏扫描

来源:互联网 发布:矩阵分析第四章答案 编辑:程序博客网 时间:2024/05/22 01:45

转载请注明出处,http://blog.csdn.net/Bule_Zst/article/details/74450685

在“使用zxing实现二维码的扫描功能”一文中已经完成了基本的扫描功能,但是是横屏的,这篇文章主要介绍了如何修改项目,实现竖屏扫描功能。


找到CameraConfigurationManager类,类的位置为:com.google.zxing.client.android.camera;

  • 替换initFromCameraParameters方法
void initFromCameraParameters(OpenCamera camera) {    Camera.Parameters parameters = camera.getCamera().getParameters();    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);    Display display = manager.getDefaultDisplay();    Point theScreenResolution = new Point();    display.getSize(theScreenResolution);    screenResolution = theScreenResolution;    Log.i(TAG, "Screen resolution: " + screenResolution);    //修改竖屏显示,解决图形拉伸问题    Point p = new Point();    p.x = this.screenResolution.x;    p.y = this.screenResolution.y;    if (this.screenResolution.x < this.screenResolution.y) {      p.x = this.screenResolution.y;      p.y = this.screenResolution.x;    }    cameraResolution = CameraConfigurationUtils.findBestPreviewSizeValue(parameters, p);    Log.i(TAG, "Camera resolution: " + cameraResolution);  }
  • 修改setDesiredCameraParameters方法
    Camera theCamera = camera.getCamera();后添加代码
    theCamera.setDisplayOrientation( 90 );

替换CameraManager类中的getFramingRectInPreview方法,类的位置:com.google.zxing.client.android.camera;

public synchronized Rect getFramingRectInPreview() {    if (framingRectInPreview == null) {      Rect framingRect = getFramingRect();      if (framingRect == null) {        return null;      }      Rect rect = new Rect(framingRect);      Point cameraResolution = configManager.getCameraResolution();      Point screenResolution = configManager.getScreenResolution();      if (cameraResolution == null || screenResolution == null) {        // Called early, before init even finished        return null;      }      //修改竖屏,扫描框尺寸的变换      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);      framingRectInPreview = rect;    }    return framingRectInPreview;  } 

修改DecodeHandler类中的decode方法,类的位置:com.google.zxing.client.android;

PlanarYUVLuminanceSource source =
activity.getCameraManager().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 temp = width;      width = height;      height = temp;      data = rotatedData;  

将AndroidManifest主配置文件的CaptureActivity显示改为竖屏显示
android:screenOrientation="portrait"
原本是android:screenOrientation="landscape"

在CaptureActivity类中将onResume方法里的下面这段代码注释掉,类的位置:com.google.zxing.client.android;

if (prefs.getBoolean(PreferencesActivity.KEY_DISABLE_AUTO_ORIENTATION, true)) {      setRequestedOrientation(getCurrentOrientation());} else {  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);}resetStatusView();

参考文章,http://blog.csdn.net/rickyfm/article/details/49339473

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 科目一身份丢了怎么办 考驾照身份证过期了怎么办 酒驾发交通事故致人死亡怎么办 车保险快到期了怎么办 车保险贴掉了怎么办 小车撞凹进去了怎么办 被代位追偿了怎么办 车被别人抵押了怎么办 朋友没驾照借车怎么办 车子被朋友撞了怎么办 电车被交警扣了怎么办 e照扣了12分怎么办 驾驶证被扣33分怎么办 a2驾驶员扣33分怎么办 驾驶证被扣48分怎么办 驾照扣了33分怎么办 车辆被扣36分怎么办 车被朋友借走了怎么办 在中国终身禁驾怎么办 c驾驶证忘年审了怎么办 驾驶证脱审1年多怎么办 驾驶证脱审四个月怎么办 驾照b2过期没审怎么办 a照驾照过期没审怎么办 驾照扣60多分怎么办 无证违章被扣分怎么办 变味的牛奶喝了怎么办 孩子喝了坏牛奶怎么办 孕妇喝了坏牛奶怎么办 驾照过期五年了怎么办 科目一没过之后怎么办? 重庆科目一没过怎么办 科三过了没签字怎么办 驾照过期几天撞死人怎么办 办理过期身份时出错了怎么办 新加坡半年临时驾照过期后怎么办 北京怎么办残摩行驶证 报考驾照三年到期了怎么办 车辆违章扣3分怎么办 驾照过期忘审了怎么办 外省港澳证办了居住证怎么办