• 二维码识别项目zxing横屏改为竖屏


     

    第1步:

    在AndroidManifest中将CaptureActivity的screenOrientation属性做如下修改:

    android:screenOrientation="portrait" 

    第2步:

    我们要把摄像头预览景调为竖向

    CameraConfigurationManager类中的setDesiredCameraParameters()方法中添加如下代码:

    // 使摄像头旋转90度
        setDisplayOrientation(camera, 90);

    然后在CameraConfigurationManager类的最后添加setDisplayOrientation()方法:

    /*改变照相机成像的方向的方法*/
      protected void setDisplayOrientation(Camera camera, int angle) {
          Method downPolymorphic = null;        
          try {
            downPolymorphic = camera.getClass().getMethod("setDisplayOrientation", new Class[] { int.class });
            if (downPolymorphic != null)     
                  downPolymorphic.invoke(camera, new Object[]{angle});        
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    
      }

    最后在CameraConfigurationManager中的initFromCameraParameters()方法的Log.d(TAG, "Screen resolution: " + screenResolution);句后面添加如下代码,这段代码是为了解决摄像头竖过来后图像拉伸的问题:

    //为竖屏添加    
        Point screenResolutionForCamera = new Point();    
        screenResolutionForCamera.x = screenResolution.x;   
         screenResolutionForCamera.y = screenResolution.y;    
        if (screenResolution.x < screenResolution.y) {       
         screenResolutionForCamera.x = screenResolution.y;       
         screenResolutionForCamera.y = screenResolution.x;    
        }    
    //  cameraResolution = getCameraResolution(parameters, screenResolutionForCamera);
        cameraResolution = findBestPreviewSizeValue(parameters, screenResolutionForCamera);

    第3步:

    CameranManager类中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;

     

    第4部

    在DecodeHandler类中的decode(byte[] data, int width, int height)方法中,在buildLuminanceSource调用前添加如下代码:

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

    最后:

    还是不行的话,就修改PlanarYUVLuminanceSource类,如果没有这个类就从其他地方copy。

    PlanarYUVLuminanceSource类中的getRow()方法为识别条形码部分,

    getMatrix()方法为识别二维码部分

    renderCroppedGreyscaleBitmap()方法为生成获取的码图部分

     

    将getRow()中的:

    int offset = (y + top) * dataWidth + left;

    getMatrix()中的:

    int inputOffset = top * dataWidth + left;

     

    inputOffset += dataWidth;

    renderCroppedGreyscaleBitmap()中的:

    int inputOffset = top * dataWidth + left;

     

    inputOffset += dataWidth;

    这些语句中dataWidth全部替换为dataHeight

    同时将PlanarYUVLuminanceSource构造方法中:

    if (left + width > dataWidth || top + height > dataHeight) {
          throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
        }

     

    dataWidth与dateHeight中互换位置即可。

     

     

     

     

  • 相关阅读:
    电子辅助的个体化严密控制策略比常规方法更有效地帮助早期RA实现全面控制病情[EULAR2015_THU0122]
    超声和免疫学指标的特征能否反映RA临床缓解的表型?[EULAR2015_THU0121]
    依那西普减量维持过程中RA病人自报病情复发可能预示未来放射学进展[EULAR2015_SAT0147]
    RETRO研究: 持续缓解的RA患者的减量维持方案[EULAR2015_SAT0056]
    OPTIRRA研究: TNF拮抗剂维持期优化减量方案[EULAR2015_SAT0150]
    与时俱进的治疗策略不断提高RA无药缓解机会[EULAR2015_SAT0058]
    雷公藤多甙治疗类风湿关节炎遭质疑
    我的博客今天2岁203天了,我领取了先锋博主徽章
    MyEclipse中最常用的快捷键大全
    MyEclipse无法打开jsp文件(打开是空白的),但是可以打开java文件
  • 原文地址:https://www.cnblogs.com/dj168/p/3966119.html
Copyright © 2020-2023  润新知