此项目源码地址:请点击这里
项目中需要,稍微研究了一下,当然是利用谷歌的zxing进行生成的。
需要加入此依赖:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.google.zxing:core:3.3.0'
}
当时搜jcenter中有好多,加入这一个就可以了
看一下qrcode工具类,这里是仿照谷歌项目中写的,查了查资料基本都是这么写的
package com.fanyafeng.barcode.util;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import java.util.Hashtable;
/**
* Author: fanyafeng
* Data: 16/12/1 13:41
* Email: fanyafeng@live.cn
*/
public class QRCode {
//宽度值,影响中间图片大小
private static int IMAGE_HALFWIDTH = 50;
//生成二维码,默认大小500*500,网址,汉字
public static Bitmap createQRCode(String text) {
return createQRCode(text, 500);
}
/**
* 生成二维码,不带logo
*
* @param text
* @param size
* @return
*/
public static Bitmap createQRCode(String text, int size) {
try {
Hashtable<EncodeHintType, String> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = 0xff000000;
} else {
pixels[y * size + x] = 0xffffffff;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, size, 0, 0, size, size);
return bitmap;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
public static Bitmap createQRCodeWithLogo(String text, Bitmap bitmap) {
return createQRCodeWithLogo(text, 500, bitmap);
}
public static Bitmap createQRCodeWithLogo(String text, int size, Bitmap bitmap) {
try {
IMAGE_HALFWIDTH = size / 10;
Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix bitMatrix = new QRCodeWriter().encode(text, BarcodeFormat.QR_CODE, size, size, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
int halfW = width / 2;
int halfH = height / 2;
Matrix m = new Matrix();
float sx = (float) 2 * IMAGE_HALFWIDTH / bitmap.getWidth();
float sy = (float) 2 * IMAGE_HALFWIDTH / bitmap.getHeight();
m.setScale(sx, sy);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, false);
int[] pixels = new int[size * size];
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH &&
y > halfH - IMAGE_HALFWIDTH && y < halfH + IMAGE_HALFWIDTH) {
pixels[y * width + x] = bitmap.getPixel(x - halfW + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH);
} else {
if (bitMatrix.get(x, y)) {
pixels[y * size + x] = 0xff000000;
} else {
pixels[y * size + x] = 0xffffffff;
}
}
}
}
Bitmap bitmap1 = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
bitmap1.setPixels(pixels, 0, size, 0, 0, size, size);
return bitmap1;
} catch (WriterException e) {
e.printStackTrace();
return null;
}
}
}
二维码的生成使用这个工具类就可以了:
package com.fanyafeng.barcode.activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.fanyafeng.barcode.R;
import com.fanyafeng.barcode.BaseActivity;
import com.fanyafeng.barcode.util.QRCode;
import com.google.zxing.common.StringUtils;
//需要搭配baseactivity,这里默认为baseactivity,并且默认Baseactivity为包名的根目录
public class MainActivity extends BaseActivity {
private ImageView ivBarCode;
private ImageView ivLogoBarCode;
private EditText etInputMessage;
private ImageView ivCreateCode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//这里默认使用的是toolbar的左上角标题,如果需要使用的标题为中心的采用下方注释的代码,将此注释掉即可
title = getString(R.string.app_name);
isSetNavigationIcon = false;
initView();
initData();
}
//初始化UI空间
private void initView() {
ivBarCode = (ImageView) findViewById(R.id.ivBarCode);
ivLogoBarCode = (ImageView) findViewById(R.id.ivLogoBarCode);
etInputMessage = (EditText) findViewById(R.id.etInputMessage);
ivCreateCode = (ImageView) findViewById(R.id.ivCreateCode);
}
//初始化数据
private void initData() {
Bitmap bitmap = QRCode.createQRCode("樊亚风", 500);
ivBarCode.setImageBitmap(bitmap);
Bitmap bitmap1 = QRCode.createQRCodeWithLogo("http://news.ifeng.com/a/20161201/50345907_0.shtml", 500, BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
ivLogoBarCode.setImageBitmap(bitmap1);
}
@Override
public void onClick(View v) {
super.onClick(v);
switch (v.getId()) {
case R.id.btnScanCode:
startActivity(new Intent(this, ScanCodeActivity.class));
break;
case R.id.btnCreateCode:
String inputString = etInputMessage.getText().toString().trim();
if (inputString != null && !inputString.equals("")) {
Bitmap bitmap = QRCode.createQRCode(inputString, 500);
ivCreateCode.setImageBitmap(bitmap);
}
break;
}
}
}