本文轉自: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));
- }
- }}



