Android 把url生成二维码并贴到给定的底图上

主要是用到了com.google.zxing jar包生成二维码的功能,这个jar包需要自己接下载 

直接上代码

    public static Bitmap CreateBinaryCodeImageByUrl(String url,Bitmap bottomImg,int drawAtPointX,int drawAtPointY,int binaryCodeImgWidth)
    {
        try{
            Map<EncodeHintType, String> hints = new HashMap<EncodeHintType,String>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(url,BarcodeFormat.QR_CODE,binaryCodeImgWidth,binaryCodeImgWidth,hints);           
            int width = bottomImg.getWidth();
            int height = bottomImg.getHeight();
            Bitmap targetBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    
            for (int posX = 0; posX < width; posX++ ){
                for( int posY = 0; posY < height; posY++){
                    targetBmp.setPixel(posX, posY, bottomImg.getPixel(posX, posY));
                }
            }
            
            int limitWidth = drawAtPointX + binaryCodeImgWidth;
            int limitHeight = drawAtPointY + binaryCodeImgWidth;
            for ( int posX = drawAtPointX, matrixX = 0; posX < limitWidth; posX++,matrixX++){
                for( int posY = drawAtPointY, matrixY=0; posY < limitHeight; posY++,matrixY++){
                    targetBmp.setPixel(posX, posY, bitMatrix.get(matrixX, matrixY)?0x000000FF:0xFFFFFFFF);
                }
            }
            
            return targetBmp;
        }
        catch(Exception e){return null;
        }
    }

参数说明:url 就是需要转换成二维码的图片,bottomImg 就是底图,drawAtPositionX 和 drawAtPositionY 表示二维码在地图上开始绘制的坐标点(底图左上角为坐标原点)

binaryCodeImgWidth 表示生成二维码的边长

放上两个bitmap 的生成方法

//截图后保存的图片地址

Bitmap screenshotImg = BitmapFactory.decodeFile(imagePath);

//直接从资源包中取图片

InputStream shareImgStream = instance.getResources().getAssets().open(“res/xx.png”);

BitMAP screenshotImg =  BitmapFactory.decodeStream(shareImgStream);

效果如下

参考:

https://www.cnblogs.com/hongten/archive/2012/10/26/java_qrcode.html

http://www.cnblogs.com/mfrbuaa/p/5068162.html

原文地址:https://www.cnblogs.com/abelmou/p/9929376.html