欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

使用Zxing实现二维码生成器内嵌图片

程序员文章站 2024-03-11 22:20:31
使用zxing实现二维码生成器内嵌图片,具有一定的参考价值,具体如下: 基本思路是先使用zxing生成的二维码图片,然后读取图片,在其中插入图标,然后整个输出图片。...

使用zxing实现二维码生成器内嵌图片,具有一定的参考价值,具体如下:

基本思路是先使用zxing生成的二维码图片,然后读取图片,在其中插入图标,然后整个输出图片。

最近的项目中需要生成二维码,找了几个例子综合下,做出了最后的效果,二维码可以生成图片格式(jpg等)或者在web页面上显示,此片文章仅作记录,雷同之处多多,包涵。。。。

注:需要zxing包装的工具类,大概的流程是读取内嵌的图片,将内容转化成二维码,将图片内嵌到二维码中,出图。

下面是完整代码:

import java.awt.basicstroke;
import java.awt.color;
import java.awt.graphics2d;
import java.awt.image;
import java.awt.shape;
import java.awt.geom.affinetransform;
import java.awt.geom.roundrectangle2d;
import java.awt.image.affinetransformop;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import java.util.hashmap;
import java.util.map;


import javax.imageio.imageio;


import com.google.zxing.barcodeformat;
import com.google.zxing.encodehinttype;
import com.google.zxing.multiformatwriter;
import com.google.zxing.writerexception;
import com.google.zxing.common.bitmatrix;
import com.google.zxing.qrcode.decoder.errorcorrectionlevel;


public class zxing {


private static final int black = 0xff000000;
private static final int white = 0xffffffff;
// 图片宽度的一般 
 private static final int image_width = 80; 
 private static final int image_height = 80; 
 private static final int image_half_width = image_width / 2; 
 private static final int frame_width = 2; 
 
 // 二维码写码器 
 private static multiformatwriter mutiwriter = new multiformatwriter(); 
public static void main(string[] args) {
try {
//bitmatrix bitmatrix = multiformatwriter.encode(content, barcodeformat.qr_code, 400, 400,hints);
string content="13400000000";//二维码的内容
bufferedimage image = genbarcode(content, 400, 400, "f:\\amazed.png");
  if (!imageio.write(image, "jpg", new file("f:\\2122.jpg"))) {
   throw new ioexception("could not write an image of format ");
  }

          /**

           //将上面的代码换成此处,使用流读入到页面即可

outputstream os = response.getoutputstream();

 if (!imageio.write(image, "jpg",os)) {

       throw new ioexception("could not write an image of format ");

      }

          **/
} catch (writerexception e) {
e.printstacktrace();
} catch (ioexception e) {
// todo auto-generated catch block
e.printstacktrace();
}
}

private bufferedimage tobufferedimage(bitmatrix matrix) {
int width = matrix.getwidth();
int height = matrix.getheight();
bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setrgb(x, y, matrix.get(x, y) ? black : white);
}
}
return image;
}
private static bufferedimage genbarcode(string content, int width, 
     int height, string srcimagepath) throws writerexception, 
     ioexception { 
   // 读取源图像 
   bufferedimage scaleimage = scale(srcimagepath, image_width, 
       image_height, true); 
   int[][] srcpixels = new int[image_width][image_height]; 
   for (int i = 0; i < scaleimage.getwidth(); i++) { 
     for (int j = 0; j < scaleimage.getheight(); j++) { 
       srcpixels[i][j] = scaleimage.getrgb(i, j); 
     } 
   } 
 
   map<encodehinttype, object> hint = new hashmap<encodehinttype, object>(); 
   hint.put(encodehinttype.character_set, "utf-8"); //内容编码
   hint.put(encodehinttype.error_correction, errorcorrectionlevel.h);//错误等级
   hint.put(encodehinttype.margin, 1); //设置二维码外边框的空白区域的宽度
   // 生成二维码 
   bitmatrix matrix = mutiwriter.encode(content, barcodeformat.qr_code, 
       width, height, hint); 
 
   // 二维矩阵转为一维像素数组 
   int halfw = matrix.getwidth() / 2; 
   int halfh = matrix.getheight() / 2; 
   int[] pixels = new int[width * height]; 
 
   for (int y = 0; y < matrix.getheight(); y++) { 
     for (int x = 0; x < matrix.getwidth(); x++) { 
       // 读取图片 
       if (x > halfw - image_half_width 
           && x < halfw + image_half_width 
           && y > halfh - image_half_width 
           && y < halfh + image_half_width) { 
         pixels[y * width + x] = srcpixels[x - halfw 
             + image_half_width][y - halfh + image_half_width]; 
       }  
       // 在图片四周形成边框 
       else if ((x > halfw - image_half_width - frame_width 
           && x < halfw - image_half_width + frame_width 
           && y > halfh - image_half_width - frame_width && y < halfh 
           + image_half_width + frame_width) 
           || (x > halfw + image_half_width - frame_width 
               && x < halfw + image_half_width + frame_width 
               && y > halfh - image_half_width - frame_width && y < halfh 
               + image_half_width + frame_width) 
           || (x > halfw - image_half_width - frame_width 
               && x < halfw + image_half_width + frame_width 
               && y > halfh - image_half_width - frame_width && y < halfh 
               - image_half_width + frame_width) 
           || (x > halfw - image_half_width - frame_width 
               && x < halfw + image_half_width + frame_width 
               && y > halfh + image_half_width - frame_width && y < halfh 
               + image_half_width + frame_width)) { 
         pixels[y * width + x] = 0xfffffff; 
       } else { 
         // 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色; 
         pixels[y * width + x] = matrix.get(x, y) ? 0xff000000 
             : 0xfffffff; 
       } 
     } 
   } 
 
   bufferedimage image = new bufferedimage(width, height, 
       bufferedimage.type_int_rgb); 
   image.getraster().setdataelements(0, 0, width, height, pixels); 
 
   return image; 
 } 
 
 /** 
  * 把传入的原始图像按高度和宽度进行缩放,生成符合要求的图标 
  * 
  * @param srcimagefile 
  *      源文件地址 
  * @param height 
  *      目标高度 
  * @param width 
  *      目标宽度 
  * @param hasfiller 
  *      比例不对时是否需要补白:true为补白; false为不补白; 
  * @throws ioexception 
  */ 
 private static bufferedimage scale(string srcimagefile, int height, 
     int width, boolean hasfiller) throws ioexception { 
   double ratio = 0.0; // 缩放比例 
   file file = new file(srcimagefile); 
   bufferedimage srcimage = imageio.read(file); 
   image destimage = srcimage.getscaledinstance(width, height, 
       bufferedimage.scale_smooth); 
   // 计算比例 
   if ((srcimage.getheight() > height) || (srcimage.getwidth() > width)) { 
     if (srcimage.getheight() > srcimage.getwidth()) { 
       ratio = (new integer(height)).doublevalue() 
           / srcimage.getheight(); 
     } else { 
       ratio = (new integer(width)).doublevalue() 
           / srcimage.getwidth(); 
     } 
     affinetransformop op = new affinetransformop( 
         affinetransform.getscaleinstance(ratio, ratio), null); 
     destimage = op.filter(srcimage, null); 
   } 
   if (hasfiller) {// 补白 
     bufferedimage image = new bufferedimage(width, height, 
         bufferedimage.type_int_rgb); 
     graphics2d graphic = image.creategraphics(); 
     graphic.setcolor(color.pink); 
     graphic.fillrect(10, 10, width, height); 
     graphic.drawrect(100, 360, width, height);
     if (width == destimage.getwidth(null)) { 
       graphic.drawimage(destimage, 0, 
           (height - destimage.getheight(null)) / 2, 
           destimage.getwidth(null), destimage.getheight(null), 
           color.white, null); 
       shape shape = new roundrectangle2d.float(0, (height - destimage.getheight(null)) / 2, width, width, 20, 20);
       graphic.setstroke(new basicstroke(5f));
       graphic.draw(shape);
     }
     else {
       graphic.drawimage(destimage, 
           (width - destimage.getwidth(null)) / 2, 0, 
           destimage.getwidth(null), destimage.getheight(null), 
           color.white, null);
       shape shape = new roundrectangle2d.float((width - destimage.getwidth(null)) / 2, 0, width, width, 20, 20);
       graphic.setstroke(new basicstroke(5f));
       graphic.draw(shape);
     }
     graphic.dispose(); 
     destimage = image; 
     
   } 
   return (bufferedimage) destimage; 
 } 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。