本文转自: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. }}

更多相关文章

  1. 利用JDBC连接服务器数据库(Android)
  2. Android学习之Android自带例子 ContactManager
  3. Android应用数据存储几种方式(3)
  4. Android混淆代码
  5. Android 下通过jni读取i2c数据
  6. Android热修复原理(一)热修复框架对比和代码修复
  7. Android动态刷新listview中的数据

随机推荐

  1. android修改AlertDialog主题
  2. Android(安卓)系统版本
  3. Android(安卓)用源代码写layout布局
  4. Android(安卓)各种音量的获取和设置
  5. Android监听键盘上的确定键等
  6. Android(安卓)数据库操作
  7. Android的Bitmap类中常用方法
  8. 安卓 跳转淘宝、京东app,并打开商品详情
  9. android面试题汇总
  10. android在处理一写图片资源