[轉]android條形碼編解碼


本文轉自:http://daixj110.iteye.com/blog/1459173

 

對條形碼的編解碼可以用barcode庫和zxing庫,但對於android ,barcode庫中的BufferedImage不能使用,我所看到的用得較多的是zxing庫,地址在http://code.google.com/p/zxing/ 里面有庫的源碼與幾種平台的例子。里面的例子只支持橫屏模式下,要支持豎屏得對其進行修改。步驟如下:

1.在DecodeHandler.java中,修改decode方法
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;

PlanarYUVLuminanceSource source = CameraManager.get().buildLuminanceSource(rotatedData, width, height);

2.在CameraManager.java中,注釋代碼:
// 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;

3.在CameraConfigurationManager.java中,在setDesiredCameraParameters方法中添加一句
camera.setDisplayOrientation(90);

4.在AndroidManifest.xml中,把Activity的屬性android:screenOrientation="landscape"
改為
android:screenOrientation="portrait"

編譯運行即可!

 

參考:

http://code.google.com/p/zxing/issues/detail?id=178#c46

 

代碼:

https://github.com/pplante/zxing-android

 

 

解碼實際是對攝像頭獲得的數據的yuv數據進行解碼

注:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 設置后或者在mainfest配置文件中landscape后,onconfigruationChanged不被調用,所以不能用來判斷橫豎屏

 

對於解碼要支持橫豎屏得涉及方向的判斷,這兒有幾篇文章可以參考:

 

yuv數據的轉換

http://chenweihuacwh.iteye.com/blog/571223

通過senser獲得屏幕旋轉

http://blog.csdn.net/a345017062/article/details/6592527

傳感器設計的指南針

http://blog.csdn.net/tinya0913/article/details/6057637

通過orientationeventListener判斷手機橫豎指向

http://androidbiancheng.blogspot.jp/2011/05/orientationeventlistener.html

 

 

Java代碼 復制代碼 收藏代碼
  1. package com.AndroidOrientation;import android.app.Activity;
  2. import android.content.Context;
  3. import android.hardware.SensorManager;
  4. import android.os.Bundle;
  5. import android.view.OrientationEventListener;
  6. import android.widget.TextView;
  7. import android.widget.Toast;
  8. public class AndroidOrientation extends Activity{
  9. TextView orientation;
  10. MyOrientationEventListener myOrientationEventListener;
  11. /** Called when the activity is first created. */
  12. @Override
  13. public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
  14. setContentView(R.layout.main);
  15. orientation = (TextView)findViewById(R.id.orientation); myOrientationEventListener = new MyOrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL);
  16. if (myOrientationEventListener.canDetectOrientation()){ myOrientationEventListener.enable();
  17. }else{
  18. Toast.makeText(AndroidOrientation.this, "Can't DetectOrientation!", Toast.LENGTH_LONG).show();
  19. } }
  20. @Override
  21. protected void onDestroy() {
  22. // TODO Auto-generated method stub
  23. super.onDestroy();
  24. myOrientationEventListener.disable();}
  25. class MyOrientationEventListener extends OrientationEventListener{ public MyOrientationEventListener(Context context, int rate) {
  26. super(context, rate);
  27. // TODO Auto-generated constructor stub }
  28. @Override public void onOrientationChanged(int arg0) {
  29. // TODO Auto-generated method stub
  30. orientation.setText(String.valueOf(arg0));
  31. }
  32. }}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM