zxing生成二维码
implementation 'com.google.zxing:core:3.3.1' implementation(name: 'zxing-1.0.1', ext: 'aar')
implementation 'com.github.bumptech.glide:glide:4.9.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
private Bitmap getCodeBitmap(String content, int logoId) {BitmapFactory.Options opt = new BitmapFactory.Options();opt.inPreferredConfig = Bitmap.Config.RGB_565;opt.inPurgeable = true;opt.inInputShareable = true;// 获取资源图片 // @SuppressLint("ResourceType") InputStream is = getResources().openRawResource(logoId); // Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(content, (int) getResources().getDimension(R.dimen.normal_200dp), getResources().getColor(R.color.color_333), null/*BitmapFactory.decodeStream(is, null, opt)*/); // Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(content, (int) getResources().getDimension(R.dimen.normal_200dp), getResources().getColor(R.color.color_333), BitmapFactory.decodeStream(is, null, opt));Bitmap bitmap = QRCodeEncoder.syncEncodeQRCode(content, (int) getResources().getDimension(R.dimen.normal_290dp), getResources().getColor(R.color.color_333));return bitmap;}
Glide.with(gsewmimg).load(getCodeBitmap(response.data.skip, R.mipmap.zhifuicon)).into(gsewmimg); //gsewmimg.setImageBitmap(getCodeBitmap(response.data.skip, R.mipmap.zhifuicon));
方法二:
添加zxing依赖:
compile 'cn.yipianfengye.android:zxing-library:2.2'
Bitmap mBitmap = CodeUtils.createImage("sssss77777", 400, 400, null);//无logo
mBitmap = CodeUtils.createImage("ssss6666", 400, 400, BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));//logo
imageView.setImageBitmap(mBitmap);
方法一:
zxing.jar下载地址:https://pan.baidu.com/s/1_XLBnj7daO6bzP-tlRwKzg
下载下来复制到libs文件夹下,并Add As Library
<ImageViewandroid:id="@+id/iv_qrcode"android:scaleType="fitXY"android:layout_width="150dp"android:layout_height="150dp"/>
ImageView ivQrCode = (ImageView)findViewById(R.id.iv_qrcode);Bitmap bitmap = strTo2DCode("aabbbb", 800, 800);ivQrCode.setImageBitmap(addLogo(bitmap,BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)));//有logo// ivQrCode.setImageBitmap(bitmap);//无logo
1、生成二维码方法:
/*** 字符串生成二维码** @param strEncode 要生成二维码的字符串* @param width 要生成图片的宽度* @param height 要生成图片的高度* @return*/
public static Bitmap strTo2DCode(String strEncode, int width, int height) {Bitmap bitmap = null;try {BitMatrix bitMatrix = new MultiFormatWriter().encode(new String(strEncode.getBytes("UTF-8"), "iso-8859-1"), BarcodeFormat.QR_CODE, width, height);// 二维矩阵转为一维像素数组,也就是一直横着排了int[] pixels = new int[width * height];for (int y = 0; y < height; y++) {for (int x = 0; x < width; x++) {if (bitMatrix.get(x, y)) {pixels[y * width + x] = 0xff000000;} else {pixels[y * width + x] = 0x00000000;}}}bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 通过像素数组生成bitmap,具体参考apibitmap.setPixels(pixels, 0, width, 0, 0, width, height);} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (WriterException e) {e.printStackTrace();}return bitmap;
}
2、向二维码中心添加logo方法:
/*** 在二维码中间添加Logo图案*/
private static Bitmap addLogo(Bitmap src, Bitmap logo) {if (src == null) {return null;}if (logo == null) {return src;}//获取图片的宽高int srcWidth = src.getWidth();int srcHeight = src.getHeight();int logoWidth = logo.getWidth();int logoHeight = logo.getHeight();if (srcWidth == 0 || srcHeight == 0) {return null;}if (logoWidth == 0 || logoHeight == 0) {return src;}//logo大小为二维码整体大小的1/5float scaleFactor = srcWidth * 1.0f / 5/ logoWidth;Bitmap bitmap = Bitmap.createBitmap(srcWidth, srcHeight, Bitmap.Config.ARGB_8888);try {Canvas canvas = new Canvas(bitmap);canvas.drawBitmap(src, 0, 0, null);canvas.scale(scaleFactor, scaleFactor, srcWidth / 2, srcHeight / 2);canvas.drawBitmap(logo, (srcWidth - logoWidth) / 2, (srcHeight - logoHeight) / 2, null);canvas.save();canvas.restore();} catch (Exception e) {bitmap = null;e.getStackTrace();}return bitmap;
}
3、条形码
添加依赖
implementation "com.google.zxing:core:3.3.1"
工具方法
public class CodeUtils {/*** 生成条形码(不支持中文)** @param content* @return*/public static Bitmap createBarcode(String content) {try {BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.CODE_128, 3000, 700);int width = bitMatrix.getWidth();int height = bitMatrix.getHeight();int[] pixels = new int[width * height];for (int y = 0; y < height; y++) {int offset = y * width;for (int x = 0; x < width; x++) {pixels[offset + x] = bitMatrix.get(x, y) ? 0xff000000 : 0xFFFFFFFF;}}Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;} catch (WriterException e) {e.printStackTrace();}return null;}/*** 生成二维码** @param content* @return*/public static Bitmap createQrcode(String content) {Map<EncodeHintType, Object> hints = new HashMap<>();// 支持中文配置hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);try {BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 1000, 1000, hints);int width = bitMatrix.getWidth();int height = bitMatrix.getHeight();int[] pixels = new int[width * height];for (int y = 0; y < height; y++) {int offset = y * width;for (int x = 0; x < width; x++) {pixels[offset + x] = bitMatrix.get(x, y) ? 0xff000000 : 0xFFFFFFFF;}}Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);bitmap.setPixels(pixels, 0, width, 0, 0, width, height);return bitmap;} catch (WriterException e) {e.printStackTrace();}return null;}}
调用
ImageView ivBarcode = findViewById(R.id.iv_barcode);
ImageView ivQrcode = findViewById(R.id.iv_qrcode);
ivBarcode.setImageBitmap(CodeUtils.createBarcode("This is a barcode"));//条形码
ivQrcode.setImageBitmap(CodeUtils.createQrcode("This is a qrcode"));//二维码