zxing条形码扫描
参考http://blog.csdn.net/hellogv/article/details/6101663
使用zxing的第三方包. http://code.google.com/p/zxing
请引入一个core.jar和PlanarYUVLuminanceSource.java文件. 下载附件并打开
代码如下:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.camera" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".CameraActivity" android:screenOrientation="landscape" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.CAMERA"></uses-permission> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /></manifest>
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator"> <alpha android:fromAlpha="1" android:toAlpha="0" android:duration="2000" android:repeatCount="-1"/> </set>
<?xml version="1.0" encoding="utf-8"?> <FrameLayout android:id="@+id/FrameLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <SurfaceView android:layout_height="fill_parent" android:id="@+id/sfvCamera" android:layout_width="fill_parent"></SurfaceView> <RelativeLayout android:id="@+id/RelativeLayout01" android:layout_height="fill_parent" android:layout_width="fill_parent"> <ImageView android:id="@+id/ImageView01" android:layout_height="100dip" android:layout_width="160dip"></ImageView> <View android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:layout_width="300dip" android:background="#55FF6666" android:id="@+id/centerView" android:layout_height="180dip"></View> <TextView android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_below="@+id/centerView" android:layout_height="wrap_content" android:text="Scanning..." android:id="@+id/txtScanResult" android:textColor="#FF000000"></TextView> </RelativeLayout> <View android:id="@+id/animationLine" android:layout_width="fill_parent" android:layout_height="2px" android:background="#FF9790" android:layout_gravity="center_vertical"></View> </FrameLayout>
package com.camera;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Hashtable;import java.util.Timer;import java.util.TimerTask;import android.app.Activity;import android.content.pm.ActivityInfo;import android.graphics.Bitmap;import android.hardware.Camera;import android.os.Bundle;import android.view.KeyEvent;import android.view.SurfaceView;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.ImageView;import android.widget.TextView;import com.google.zxing.BinaryBitmap;import com.google.zxing.DecodeHintType;import com.google.zxing.MultiFormatReader;import com.google.zxing.Result;import com.google.zxing.common.HybridBinarizer;import com.util.PlanarYUVLuminanceSource;import com.util.SFHCamera;public class CameraActivity extends Activity { /** Called when the activity is first created. */ private SurfaceView sfvCamera; private SFHCamera sfhCamera; private ImageView imgView; private View centerView; private TextView txtScanResult; private Timer mTimer; private MyTimerTask mTimerTask; // 按照标准HVGA final static int width = 480; final static int height = 320; int dstLeft, dstTop, dstWidth, dstHeight; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.main); this.setTitle("Android条码/二维码识别Demo-----hellogv"); imgView = (ImageView) this.findViewById(R.id.ImageView01); centerView = (View) this.findViewById(R.id.centerView); sfvCamera = (SurfaceView) this.findViewById(R.id.sfvCamera); sfhCamera = new SFHCamera(sfvCamera.getHolder(), width, height, previewCallback); txtScanResult=(TextView)this.findViewById(R.id.txtScanResult); // 初始化定时器 mTimer = new Timer(); mTimerTask = new MyTimerTask(); mTimer.schedule(mTimerTask, 0, 2000); Animation animation = AnimationUtils.loadAnimation(this, R.anim.alph); ((View)findViewById(R.id.animationLine)).startAnimation(animation); } class MyTimerTask extends TimerTask { @Override public void run() { if (dstLeft == 0) {//只赋值一次 dstLeft = centerView.getLeft() * width / getWindowManager().getDefaultDisplay().getWidth(); dstTop = centerView.getTop() * height / getWindowManager().getDefaultDisplay().getHeight(); dstWidth = ((centerView.getRight() - centerView.getLeft())* width / getWindowManager().getDefaultDisplay().getWidth()); dstHeight = (centerView.getBottom() - centerView.getTop())* height / getWindowManager().getDefaultDisplay().getHeight(); System.out.println("dstLeft:"+dstLeft+" dstTop:"+dstTop+" dstWidth:"+dstWidth+" dstHeight:"+dstHeight); sfhCamera.AutoFocusAndPreviewCallback(); } } } /** * 自动对焦后输出图片 */ private Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() { @Override public void onPreviewFrame(byte[] data, Camera camera) { //取得指定范围的帧的数据 PlanarYUVLuminanceSource source = new PlanarYUVLuminanceSource(data, width, height, dstLeft, dstTop, dstWidth, dstHeight,false); //取得灰度图 Bitmap mBitmap = source.renderCroppedGreyscaleBitmap(); //显示灰度图 imgView.setImageBitmap(mBitmap); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); MultiFormatReader reader = new MultiFormatReader(); try { saveImage(mBitmap); Hashtable hints = new Hashtable(); hints.put(DecodeHintType.CHARACTER_SET, "UTF-8"); Result result = reader.decode(bitmap,hints); String strResult = "BarcodeFormat:" + result.getBarcodeFormat().toString() + " text:" + result.getText(); txtScanResult.setText(strResult); } catch (Exception e) { txtScanResult.setText("Scanning"); sfhCamera.AutoFocusAndPreviewCallback(); } } }; @Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK&&event.getRepeatCount()==0){ sfhCamera.closeCamera(); }return super.onKeyDown(keyCode, event);}@Overrideprotected void onResume() {if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);}super.onResume();}private void saveImage(Bitmap mBitmap){File file=new File("/sdcard/feng.png"); try { FileOutputStream out=new FileOutputStream(file); if(mBitmap.compress(Bitmap.CompressFormat.PNG, 50, out)){ out.flush(); out.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
package com.util;import java.io.IOException; import android.graphics.PixelFormat; import android.hardware.Camera; import android.util.Log; import android.view.SurfaceHolder; public class SFHCamera implements SurfaceHolder.Callback{ private SurfaceHolder holder = null; private Camera mCamera; private int width,height; private Camera.PreviewCallback previewCallback; public SFHCamera(SurfaceHolder holder,int w,int h,Camera.PreviewCallback previewCallback) { this.holder = holder; this.holder.addCallback(this); this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); width=w; height=h; this.previewCallback=previewCallback; } @Override public void surfaceChanged(SurfaceHolder arg0, int format, int w, int h) { Camera.Parameters parameters = mCamera.getParameters(); parameters.setPreviewSize(width, height);//设置尺寸 parameters.setPictureFormat(PixelFormat.JPEG); // parameters.set("orientation", "portrait"); mCamera.setParameters(parameters); mCamera.startPreview();//开始预览 Log.e("Camera","surfaceChanged"); } @Override public void surfaceCreated(SurfaceHolder arg0) { mCamera = Camera.open();//启动服务 // mCamera.setDisplayOrientation(90); try { mCamera.setPreviewDisplay(holder);//设置预览 Log.e("Camera","surfaceCreated"); } catch (IOException e) { mCamera.release();//释放 mCamera = null; } } @Override public void surfaceDestroyed(SurfaceHolder arg0) { /* mCamera.setPreviewCallback(null); mCamera.stopPreview();//停止预览 mCamera = null; */ Log.e("Camera","surfaceDestroyed"); } /** * 自动对焦并回调Camera.PreviewCallback */ public void AutoFocusAndPreviewCallback() { if(mCamera!=null) mCamera.autoFocus(mAutoFocusCallBack); } /** * 自动对焦 */ private Camera.AutoFocusCallback mAutoFocusCallBack = new Camera.AutoFocusCallback() { @Override public void onAutoFocus(boolean success, Camera camera) { mCamera.setOneShotPreviewCallback(previewCallback); } }; public void closeCamera(){ mCamera.stopPreview(); mCamera.release(); mCamera = null; } }1 楼 web8610 2011-11-15 兄弟 这个代码缺文件 R.anim.alph R.id.animationLine 能不能发给我啊 77892906@qq.com 万分感谢 2 楼 rayln 2011-11-15 web8610 写道兄弟 这个代码缺文件 R.anim.alph R.id.animationLine 能不能发给我啊 77892906@qq.com 万分感谢