[转]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. }}
原文地址:https://www.cnblogs.com/freeliver54/p/2564364.html