本文转自: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
- package com.AndroidOrientation;import android.app.Activity;
- import android.content.Context;
- import android.hardware.SensorManager;
- import android.os.Bundle;
- import android.view.OrientationEventListener;
- import android.widget.TextView;
- import android.widget.Toast;
- public class AndroidOrientation extends Activity{
- TextView orientation;
- MyOrientationEventListener myOrientationEventListener;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- orientation = (TextView)findViewById(R.id.orientation); myOrientationEventListener = new MyOrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL);
- if (myOrientationEventListener.canDetectOrientation()){ myOrientationEventListener.enable();
- }else{
- Toast.makeText(AndroidOrientation.this, "Can't DetectOrientation!", Toast.LENGTH_LONG).show();
- } }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- myOrientationEventListener.disable();}
- class MyOrientationEventListener extends OrientationEventListener{ public MyOrientationEventListener(Context context, int rate) {
- super(context, rate);
- // TODO Auto-generated constructor stub }
- @Override public void onOrientationChanged(int arg0) {
- // TODO Auto-generated method stub
- orientation.setText(String.valueOf(arg0));
- }
- }}