修改ZXing for Android為豎屏模式

來源:互聯網
上載者:User

最近在開發一個二維碼掃描的項目,Down了一份Zxing的源碼然後進行改寫,在修改Zxing for android豎屏模式,遇到如下問題:

在修改了AndroidMainfest.xml中的Activity的屬性android:screenOrientation="portrait"後,Activity被強制設定為豎屏模式,但是當啟動攝像機的時候,顯示的畫面仍然為橫屏模式。故在網上找了如下方法:

針對ZXing 1.6版本:

1、修改manifest檔案,將CaptureActivity設為portrait

android:screenOrientation="portrait"

2、在DecodeHandler.java檔案中,找到decode(byte[],int,int)方法,在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 #11width = height;height = tmp;data = rotatedData;

3、在CameraManager.java中找到getFramingRectInPreview()方法, 替換相應代碼:

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、在CameraConfigurationManager.java裡找到setDesiredCameraParameters()方法,加入代碼:(這裡不同的SDK版本可能調用的方法不一樣)

camera.setDisplayOrientation(90);
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.