首先需要下载谷歌提供的二维码工具包Zxing-core-3.2.1.jar 网上有很多下载,可以自行下载。
也是根据网上的一篇文章,然后再进行了优化,由于忘记了是哪一篇文章,若是作者看到了,请留言,我加上出处。
下面我直接上代码
public class HomeUtils { /** * 生成二维码 * @param content * @param width * @param height * @return */ public static Bitmap getBitmap(String content, int width, int height) { QRCodeWriter qrCodeWriter = new QRCodeWriter(); Map, String> hints = new HashMap<>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); try { BitMatrix encode = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints); int space = 0; out:for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (encode.get(j, i)) { space = i; break out; } } } int newWidth = width - space * 2; int newHeight = height - space * 2; int[] pixels = new int[newWidth * newHeight]; for (int i = space; i < height - space; i++) { for (int j = space; j < width - space; j++) { if (encode.get(j, i)) { pixels[(i - space) * newWidth + (j - space)] = 0x00000000; } else { pixels[(i - space) * newWidth + (j - space)] = 0xffffffff; } } } return Bitmap.createBitmap(pixels, 0, newWidth, newWidth, newHeight, Bitmap.Config.RGB_565); } catch (WriterException e) { e.printStackTrace(); } return null; } /** * 在二维码中加入logo * @param qrBitmap * @param logoBitmap * @return */ public static Bitmap addLogo(Bitmap qrBitmap, Bitmap logoBitmap) { int qrBitmapWidth = qrBitmap.getWidth(); int qrBitmapHeight = qrBitmap.getHeight(); int logoBitmapWidth = logoBitmap.getWidth(); int logoBitmapHeight = logoBitmap.getHeight(); Bitmap blankBitmap = Bitmap.createBitmap(qrBitmapWidth, qrBitmapHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(blankBitmap); canvas.drawBitmap(qrBitmap, 0, 0, null); canvas.save(Canvas.ALL_SAVE_FLAG); float scaleSize = 1.0f; while ((logoBitmapWidth / scaleSize) > (qrBitmapWidth / 5) || (logoBitmapHeight / scaleSize) > (qrBitmapHeight / 5)) { scaleSize *= 2; } float sx = 1.0f / scaleSize; canvas.scale(sx, sx, qrBitmapWidth / 2, qrBitmapHeight / 2); canvas.drawBitmap(logoBitmap, (qrBitmapWidth - logoBitmapWidth) / 2, (qrBitmapHeight - logoBitmapHeight) / 2, null); canvas.restore(); return blankBitmap; } /** * 生成二维码弹出框 * * @param context * @param bitmap * @return */ public static AlertDialog commonDialog(Context context, Bitmap bitmap) { AlertDialog dialog; dialog = new AlertDialog.Builder(context).create(); dialog.show(); dialog.getWindow().setLayout(Util.dp2px(295), Util.dp2px(395)); dialog.getWindow().setContentView(R.layout.face_to_face_layout); dialog.getWindow().setBackgroundDrawableResource(R.color.transparent); ImageView iv_face_to_face = dialog.getWindow().findViewById(R.id.iv_face_to_face); iv_face_to_face.setImageBitmap(bitmap); dialog.getWindow().findViewById(R.id.iv_cancel_dialog).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); return dialog; } }
这里是写了个工具类。
在acitivity中调用方法
Bitmap qrBitmap= HomeUtils.getBitmap(mAdapter.getItem(position).getUrl(), Util.dp2px(255),Util.dp2px(255)); Bitmap logoBitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_dialog_logo); Bitmap bitmap=HomeUtils.addLogo(qrBitmap,logoBitmap);
HomeUtils.commonDialog(InvitePrizesActivity.this,bitmap);
其中getUrl是服务器返的url地址,这个可自行添加有个地址测试,比如www.baidu.com;
以上就是全部代码;