1、导入zxing代码和包
2、下面的类是解析二维码的主要类。
package com.gaint.nebula.interaction.ui.zxing; import java.io.IOException; import java.util.Vector; import android.app.Activity; import android.content.Intent; import android.content.res.AssetFileDescriptor; import android.graphics.Bitmap; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.os.Handler; import android.os.Vibrator; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; import com.gaint.nebula.interaction.R; import com.gaint.nebula.interaction.ui.BaseActivity; import com.google.zxing.BarcodeFormat; import com.google.zxing.Result; import com.mining.app.zxing.camera.CameraManager; import com.mining.app.zxing.decoding.CaptureActivityHandler; import com.mining.app.zxing.decoding.InactivityTimer; import com.mining.app.zxing.view.ViewfinderView; /** * Initial the camera * @author Ryan.Tang */ public class MipcaActivityCapture extends BaseActivity implements Callback { private CaptureActivityHandler handler; private ViewfinderView viewfinderView; private boolean hasSurface; private Vector<BarcodeFormat> decodeFormats; private String characterSet; private InactivityTimer inactivityTimer; private MediaPlayer mediaPlayer; private boolean playBeep; private static final float BEEP_VOLUME = 0.10f; private boolean vibrate; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_capture); //ViewUtil.addTopView(getApplicationContext(), this, R.string.scan_card); CameraManager.init(getApplication()); viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view); Button mButtonBack = (Button) findViewById(R.id.button_back); mButtonBack.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { MipcaActivityCapture.this.finish(); } }); hasSurface = false; inactivityTimer = new InactivityTimer(this); } @Override protected void onResume() { super.onResume(); SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view); SurfaceHolder surfaceHolder = surfaceView.getHolder(); if (hasSurface) { initCamera(surfaceHolder); } else { surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } decodeFormats = null; characterSet = null; playBeep = true; AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE); if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL) { playBeep = false; } initBeepSound(); vibrate = true; } @Override protected void onPause() { super.onPause(); if (handler != null) { handler.quitSynchronously(); handler = null; } CameraManager.get().closeDriver(); } @Override protected void onDestroy() { inactivityTimer.shutdown(); super.onDestroy(); } /** * ????????? * @param result * @param barcode */ public void handleDecode(Result result, Bitmap barcode) { inactivityTimer.onActivity(); playBeepSoundAndVibrate(); String resultString = result.getText(); if (resultString.equals("")) { Toast.makeText(MipcaActivityCapture.this, "Scan failed!", Toast.LENGTH_SHORT).show(); }else { Intent resultIntent = new Intent(); Bundle bundle = new Bundle(); bundle.putString("result", resultString); bundle.putParcelable("bitmap", barcode); resultIntent.putExtras(bundle); this.setResult(RESULT_OK, resultIntent); } MipcaActivityCapture.this.finish(); } private void initCamera(SurfaceHolder surfaceHolder) { try { CameraManager.get().openDriver(surfaceHolder); } catch (IOException ioe) { return; } catch (RuntimeException e) { return; } if (handler == null) { handler = new CaptureActivityHandler(this, decodeFormats, characterSet); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { if (!hasSurface) { hasSurface = true; initCamera(holder); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { hasSurface = false; } public ViewfinderView getViewfinderView() { return viewfinderView; } public Handler getHandler() { return handler; } public void drawViewfinder() { viewfinderView.drawViewfinder(); } private void initBeepSound() { if (playBeep && mediaPlayer == null) { // The volume on STREAM_SYSTEM is not adjustable, and users found it // too loud, // so we now play on the music stream. setVolumeControlStream(AudioManager.STREAM_MUSIC); mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.setOnCompletionListener(beepListener); AssetFileDescriptor file = getResources().openRawResourceFd( R.raw.beep); try { mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength()); file.close(); mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME); mediaPlayer.prepare(); } catch (IOException e) { mediaPlayer = null; } } } private static final long VIBRATE_DURATION = 200L; private void playBeepSoundAndVibrate() { if (playBeep && mediaPlayer != null) { mediaPlayer.start(); } if (vibrate) { Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE); vibrator.vibrate(VIBRATE_DURATION); } } /** * When the beep has finished playing, rewind to queue up another one. */ private final OnCompletionListener beepListener = new OnCompletionListener() { public void onCompletion(MediaPlayer mediaPlayer) { mediaPlayer.seekTo(0); } }; }3、调用此类:
Intent intent = new Intent(); intent.setClass(BaseActivity.this, MipcaActivityCapture.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == SCANNIN_GREQUEST_CODE) { if (data==null) { return; } Bitmap mBitmap = data.getParcelableExtra("bitmap"); final String result = data.getStringExtra("result"); Logger.getLogger().i(result+" -- "+mBitmap); View view = LayoutInflater.from(this).inflate(R.layout.scan, null); ImageView icon = (ImageView) view.findViewById(R.id.iv_scan_icon); TextView textView = (TextView) view.findViewById(R.id.tv_scan_title); icon.setImageBitmap(mBitmap); textView.setText(result); AlertDialog.Builder dialog = new AlertDialog.Builder(this); dialog.setTitle("扫描结果"); dialog.setView(view); dialog.setMessage(data.getDataString()); dialog.setNegativeButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // 用默认浏览器打开扫描得到的地址 Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); Uri content_url = Uri.parse(result); intent.setData(content_url); startActivity(intent); dialog.dismiss(); } }); dialog.setPositiveButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.create().show(); } super.onActivityResult(requestCode, resultCode, data); }
/** * @类功能说明: 生成二维码图片示例 */ public class CreateQRImageTest { private ImageView sweepIV; private int QR_WIDTH = 200, QR_HEIGHT = 200; /** * @方法功能说明: 生成二维码图片,实际使用时要初始化sweepIV,不然会报空指针错误 * @参数: @param url 要转换的地址或字符串,可以是中文 * @return void * @throws */ // 要转换的地址或字符串,可以是中文 public void createQRImage(String url) { try { // 判断URL合法性 if (url == null || "".equals(url) || url.length() < 1) { return; } Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = new QRCodeWriter().encode(url, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints); int[] pixels = new int[QR_WIDTH * QR_HEIGHT]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < QR_HEIGHT; y++) { for (int x = 0; x < QR_WIDTH; x++) { if (bitMatrix.get(x, y)) { pixels[y * QR_WIDTH + x] = 0xff000000; } else { pixels[y * QR_WIDTH + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT); // 显示到一个ImageView上面 sweepIV.setImageBitmap(bitmap); } catch (WriterException e) { e.printStackTrace(); } } }
http://www.cnblogs.com/weixing/archive/2013/08/28/3287120.html
ZXing开源项目Google Code地址:https://code.google.com/p/zxing/
ZXingDemo下载:ZXingDemo2013-8-25.rar
二、修改二维码代码
1、是扫描二维码的文字居中。
找到类ViewfinderView,其中widthPixels是屏幕的宽度。
paint.setColor(Color.WHITE); paint.setTextSize(TEXT_SIZE * density); paint.setAlpha(0x40); paint.setTypeface(Typeface.create("System", Typeface.BOLD)); //获取文字宽度,让其居中显示 float wtext = paint.measureText(getResources().getString(R.string.scan_text)); canvas.drawText(getResources().getString(R.string.scan_text), (widthPixels-wtext)/2, (float) (frame.bottom + (float)TEXT_PADDING_TOP *density), paint);
找到CameraManager类的getFramingRect方法,就可以看到。
public Rect getFramingRect() { Point screenResolution = configManager.getScreenResolution(); if (framingRect == null) { if (camera == null) { return null; } int width = screenResolution.x * 3 / 4; if (width < MIN_FRAME_WIDTH) { width = MIN_FRAME_WIDTH; } else if (width > MAX_FRAME_WIDTH) { width = MAX_FRAME_WIDTH; } int height = screenResolution.y * 3 / 4; if (height < MIN_FRAME_HEIGHT) { height = MIN_FRAME_HEIGHT; } else if (height > MAX_FRAME_HEIGHT) { height = MAX_FRAME_HEIGHT; } int leftOffset = (screenResolution.x - width) / 2; int topOffset = (screenResolution.y - height) / 2; framingRect = new Rect(leftOffset, topOffset, leftOffset + width, topOffset + height); Log.d(TAG, "Calculated framing rect: " + framingRect); } return framingRect; }
01
02
03
04
05
06
|
if (width
< height) { Log.i(TAG, "Display
reports portrait orientation; assuming this is incorrect" ); int temp
= width; width
= height; height
= temp; } |
01
|
camera.setDisplayOrientation( 90 ); |
01
02
03
04
|
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; |
01
02
03
04
|
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; |
01
02
03
04
05
06
07
08
09
|
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; width
= height; height
= tmp; data
= rotatedData; |
01
02
03
04
05
06
07
|
Point
screenResolutionForCamera = new Point(); screenResolutionForCamera.x
= screenResolution.x; screenResolutionForCamera.y
= screenResolution.y; if (screenResolution.x
< screenResolution.y) { screenResolutionForCamera.x
= screenResolution.y; screenResolutionForCamera.y
= screenResolution.x; } |
01
02
|
int width
= findDesiredDimensionInRange(screenResolution.x, MIN_FRAME_WIDTH, MAX_FRAME_WIDTH); int height
= findDesiredDimensionInRange(screenResolution.y,MIN_FRAME_HEIGHT, MAX_FRAME_HEIGHT); |
01
02
03
|
DisplayMetrics
metrics = context.getResources().getDisplayMetrics(); int width
= ( int )
(metrics.widthPixels * 0.6 ); int height
= ( int )
(width * 0.9 ); |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
//
画出四个角 paint.setColor(getResources().getColor(R.color.green)); //
左上角 canvas.drawRect(frame.left,
frame.top, frame.left + 15 ,frame.top
+ 5 ,
paint); canvas.drawRect(frame.left,
frame.top, frame.left + 5 ,frame.top
+ 15 ,
paint); //
右上角 canvas.drawRect(frame.right
- 15 ,
frame.top, frame.right,frame.top + 5 ,
paint); canvas.drawRect(frame.right
- 5 ,
frame.top, frame.right,frame.top + 15 ,
paint); //
左下角 canvas.drawRect(frame.left,
frame.bottom - 5 ,
frame.left + 15 ,frame.bottom,
paint); canvas.drawRect(frame.left,
frame.bottom - 15 ,
frame.left + 5 ,frame.bottom,
paint); //
右下角 canvas.drawRect(frame.right
- 15 ,
frame.bottom - 5 ,
frame.right,frame.bottom, paint); canvas.drawRect(frame.right
- 5 ,
frame.bottom - 15 ,
frame.right,frame.bottom, paint); |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
if ((i
+= 5 )
< frame.bottom - frame.top) {
/*
以下为用渐变线条作为扫描线 */
//
渐变图为矩形
//
mDrawable.setShape(GradientDrawable.RECTANGLE);
//
渐变图为线型
//
mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT);
//
线型矩形的四个圆角半径
//
// mDrawable
//
// .setCornerRadii(new float[] { 8, 8, 8, 8, 8, 8, 8, 8 });
//
位置边界
//
mRect.set(frame.left + 10, frame.top + i, frame.right - 10,
//
frame.top + 1 + i);
//
设置渐变图填充边界
//
mDrawable.setBounds(mRect);
//
画出渐变线条
//
mDrawable.draw(canvas);
/*
以下为图片作为扫描线 */
mRect.set(frame.left
- 6 ,
frame.top + i - 6 ,
frame.right + 6 ,frame.top
+ 6 +
i);
lineDrawable.setBounds(mRect);
lineDrawable.draw(canvas);
//
刷新
invalidate();
} else {
i
= 0 ;
} |
http://blog.csdn.net/xinchen200/article/details/18036695
原文:http://www.cnblogs.com/lbangel/p/4335882.html