先看效果:
输入内容,点击生成二维码:
点击logo图案:
代码:
QRCodeUtil:
package com.example.administrator.zxing;import androID.graphics.Bitmap;import androID.graphics.Canvas;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.io.fileOutputStream;import java.io.IOException;import java.util.HashMap;import java.util.Map;public class QRCodeUtil { public static boolean createQRImage(String content,int wIDthPix,int heightPix,Bitmap logoBm,String filePath) { try { if (content == null || "".equals(content)) { return false; } //配置参数 Map<EncodeHintType,Object> hints = new HashMap<>(); hints.put(EncodeHintType.CHaraCTER_SET,"utf-8"); //容错级别 hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.H); //设置空白边距的宽度// hints.put(EncodeHintType.margin,2); //default is 4 // 图像数据转换,使用了矩阵转换 BitMatrix bitMatrix = new QRCodeWriter().encode(content,barcodeFormat.QR_CODE,wIDthPix,heightPix,hints); int[] pixels = new int[wIDthPix * heightPix]; // 下面这里按照二维码的算法,逐个生成二维码的图片, // 两个for循环是图片横列扫描的结果 for (int y = 0; y < heightPix; y++) { for (int x = 0; x < wIDthPix; x++) { if (bitMatrix.get(x,y)) { pixels[y * wIDthPix + x] = 0xff000000; } else { pixels[y * wIDthPix + x] = 0xffffffff; } } } // 生成二维码图片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(wIDthPix,Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels,heightPix); if (logoBm != null) { bitmap = addlogo(bitmap,logoBm); } //必须使用compress方法将bitmap保存到文件中再进行读取。直接返回的bitmap是没有任何压缩的,内存消耗巨大! return bitmap != null && bitmap.compress(Bitmap.CompressFormat.JPEG,100,new fileOutputStream(filePath)); } catch (WriterException | IOException e) { e.printstacktrace(); } return false; } /** * 在二维码中间添加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/5 float 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,null); canvas.scale(scaleFactor,scaleFactor,srcWIDth / 2,srcHeight / 2); canvas.drawBitmap(logo,(srcWIDth - logoWIDth) / 2,(srcHeight - logoHeight) / 2,null); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); } catch (Exception e) { bitmap = null; e.getStackTrace(); } return bitmap; }}
MainActivity:
package com.example.administrator.zxing;import androID.content.Context;import androID.graphics.BitmapFactory;import androID.os.Bundle;import androID.os.Environment;import androID.support.v7.app.AppCompatActivity;import androID.vIEw.VIEw;import androID.Widget.button;import androID.Widget.CheckBox;import androID.Widget.EditText;import androID.Widget.ImageVIEw;import java.io.file;public class MainActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); //内容 final EditText contentET = (EditText) findVIEwByID(R.ID.create_qr_content); //显示二维码图片 final ImageVIEw imageVIEw = (ImageVIEw) findVIEwByID(R.ID.create_qr_iv); //是否添加logo final CheckBox addlogoCB = (CheckBox) findVIEwByID(R.ID.create_qr_addlogo); button createQrBtn = (button) findVIEwByID(R.ID.create_qr_btn); createQrBtn.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { final String filePath = getfileRoot(MainActivity.this) + file.separator + "qr_" + System.currentTimeMillis() + ".jpg"; //二维码图片较大时,生成图片、保存文件的时间可能较长,因此放在新线程中 new Thread(new Runnable() { @OverrIDe public voID run() { boolean success = QRCodeUtil.createQRImage(contentET.getText().toString().trim(),800,addlogoCB.isChecked() ? BitmapFactory.decodeResource(getResources(),R.mipmap.qr_logo) : null,filePath); if (success) { runOnUiThread(new Runnable() { @OverrIDe public voID run() { imageVIEw.setimageBitmap(BitmapFactory.decodefile(filePath)); } }); } } }).start(); } }); } //文件存储根目录 private String getfileRoot(Context context) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { file external = context.getExternalfilesDir(null); if (external != null) { return external.getabsolutePath(); } } return context.getfilesDir().getabsolutePath(); }}
布局:
activity_main:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:app="http://schemas.androID.com/apk/res-auto" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context="com.example.administrator.zxing.MainActivity"> <EditText androID:ID="@+ID/create_qr_content" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /> <linearLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal"> <button androID:ID="@+ID/create_qr_btn" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="生成二维码" /> <CheckBox androID:ID="@+ID/create_qr_addlogo" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="添加logo图案" /> </linearLayout> <ImageVIEw androID:ID="@+ID/create_qr_iv" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" /></linearLayout>
总结
以上所述是小编给大家介绍的AndroID 点击生成二维码功能实现代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android 点击生成二维码功能实现代码全部内容,希望文章能够帮你解决Android 点击生成二维码功能实现代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)