常用的二维码界面
文章图片
image.png
主要代码
【常用的二维码界面】二维码生成代码
/*展示数据*/
QRUrl = bean.getInfo().getLink();
//int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
//int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
//img_QRcode.measure(w, h);
//int height =img_QRcode.getMeasuredHeight();
//int width =img_QRcode.getMeasuredWidth();
//int width=ScreenUtils.getScreenHeight(getActivity());
int width = DensityUtil.dp2px(getActivity(), 200);
//px
/*生成二维码*/
mBitmap = QRCodeUtils.createQrCodeWithoutWhite(QRUrl, width, width);
if(mBitmap !=null){
img_QRcode.setImageBitmap(mBitmap);
}countDown(9);
工具类
package com.example.rsq.qrcodescan;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
import com.google.zxing.RGBLuminanceSource;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.journeyapps.barcodescanner.BarcodeEncoder;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 二维码功能封装,包括识别,生成二维码,扫描识别二维码
* @Author: renshiqian
* @Date: 2017/12/13
*/public class QRCodeUtils {
/**
* 生成二维码
* @param str
* @return
*/
public static Bitmap encodeAsBitmap(String str){
Bitmap bitmap = null;
BitMatrix result = null;
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
result = multiFormatWriter.encode(str, BarcodeFormat.QR_CODE, 200, 200);
// 使用 ZXing Android Embedded 要写的代码
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
bitmap = barcodeEncoder.createBitmap(result);
} catch (WriterException e){
e.printStackTrace();
} catch (IllegalArgumentException iae){
return null;
}
return bitmap;
}/**
* 识别相册图片二维码
* @param bitmap
* @return
*/
public static Result scanBitmap(Bitmap bitmap) {
if (bitmap == null) {
return null;
}
Map hints = new LinkedHashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF8");
// 解码设置编码方式为:utf-8
hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
//优化精度
hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);
//复杂模式,开启PURE_BARCODE模式
Bitmap scanBitmap = Bitmap.createBitmap(bitmap);
int px[] = new int[scanBitmap.getWidth() * scanBitmap.getHeight()];
scanBitmap.getPixels(px, 0, scanBitmap.getWidth(), 0, 0, scanBitmap.getWidth(), scanBitmap.getHeight());
RGBLuminanceSource source = new RGBLuminanceSource(scanBitmap.getWidth(), scanBitmap.getHeight(), px);
BinaryBitmap tempBitmap = new BinaryBitmap(new HybridBinarizer(source));
MultiFormatReader reader = new MultiFormatReader();
try {
return reader.decode(tempBitmap, hints);
} catch (NotFoundException e) {
Log.wtf("zcc", "scan:" + e.toString());
} finally {
reader.reset();
}
return null;
}public static String recode(String str) {
String formart = "";
try {
boolean ISO = Charset.forName("ISO-8859-1").newEncoder().canEncode(str);
if (ISO) {
formart = new String(str.getBytes("ISO-8859-1"), "GB2312");
} else {
formart = str;
}
} catch (UnsupportedEncodingException e) {
Log.wtf("zcc", "scan recode:" + e.toString());
}
return formart;
}/**
* 扫描二维码
* @param activity
* 请重写当前activity的onActivityResult()方法
* resultCode == RESULT_OK = -1 为成功扫描二维码
* Bundle bundle = data.getExtras();
* String scanResult = bundle.getString("result");
为扫描结果
*/
public static void scanQRCode(Activity activity){
// 创建IntentIntegrator对象
IntentIntegrator intentIntegrator = new IntentIntegrator(activity);
// 设置自定义扫描Activity
intentIntegrator.setCaptureActivity(CustomCaptureActivity.class);
// 开始扫描
intentIntegrator.initiateScan();
}/**
* 生成不带白边的二维码
* @param str
* @param width
* @param height
* @return
*/
public static Bitmap createQrCodeWithoutWhite(String str, int width, int height) {
try {
Hashtable hints = new Hashtable();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix matrix = new QRCodeWriter().encode(str, BarcodeFormat.QR_CODE, width, height);
matrix = deleteWhite(matrix);
//删除白边
width = matrix.getWidth();
height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0;
y < height;
y++) {
for (int x = 0;
x < width;
x++) {
if (matrix.get(x, y)) {
pixels[y * width + x] = Color.BLACK;
} else {
pixels[y * width + x] = Color.WHITE;
}
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
} catch (Exception e) {
return null;
}
}/**
* 生成带白边的二维码
* @param str
* @param width
* @param height
* @return
*/
public static Bitmap createQrCodeWithWhite(String str, int width, int height) {
Bitmap bitmap = null;
BitMatrix result = null;
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
result = multiFormatWriter.encode(str, BarcodeFormat.QR_CODE, width, height);
// 使用 ZXing Android Embedded 要写的代码
BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
bitmap = barcodeEncoder.createBitmap(result);
} catch (WriterException e){
e.printStackTrace();
} catch (IllegalArgumentException iae){
return null;
}
return bitmap;
}private static BitMatrix deleteWhite(BitMatrix matrix) {
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 1;
int resHeight = rec[3] + 1;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0;
i < resWidth;
i++) {
for (int j = 0;
j < resHeight;
j++) {
if (matrix.get(i + rec[0], j + rec[1]))
resMatrix.set(i, j);
}
}
return resMatrix;
}
}
推荐阅读
- 昨夜小楼听风
- 为什么你的路演总会超时()
- 深入理解Go之generate
- 遗憾是生活的常态,但孝顺这件事,我希望每一个人都不留遗憾
- 标签、语法规范、内联框架、超链接、CSS的编写位置、CSS语法、开发工具、块和内联、常用选择器、后代元素选择器、伪类、伪元素。
- 唱歌教学(导致嗓音损坏的几个常见的错误唱歌方法!)
- 迷茫是人生常态
- Hive常见问题汇总
- 数组常用方法一
- 【原创】君子之心,常怀敬畏