一、導入zxing代碼和包php
二、下面的類是解析二維碼的主要類。html
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); } }; }三、調用此類:
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
java
ZXing開源項目Google Code地址:https://code.google.com/p/zxing/android
ZXingDemo下載:ZXingDemo2013-8-25.rar 算法
2、修改二維碼代碼canvas
一、是掃描二維碼的文字居中。瀏覽器
找到類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方法,就能夠看到。app
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
eclipse