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

java生成彩色附logo二维码

程序员文章站 2024-03-11 14:43:01
java生成二维码有很多开发的jar包如zxing是谷歌开发的,这里的话我使用zxing的开发包来实现的。我们在很多项目中需要动态生成二维码,来提供给用户,这样让更多人能够...

java生成二维码有很多开发的jar包如zxing是谷歌开发的,这里的话我使用zxing的开发包来实现的。我们在很多项目中需要动态生成二维码,来提供给用户,这样让更多人能够很好的通过二维码来体验自己的应用。

下面贴出代码,已经测试通过,大家可以直接复制代码使用:

最后实现结果:

java生成彩色附logo二维码

java生成二维码

代码如下:

import java.awt.color;
import java.awt.graphics2d;
import java.awt.image;
import java.awt.geom.affinetransform;
import java.awt.image.affinetransformop;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import java.io.unsupportedencodingexception;
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 matrixtoimagewriter {
 private static final int image_width = 100;
 private static final int image_height = 100;
 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 encode(string content, int width, int height,
   string srcimagepath, string destimagepath) {
  try {
   imageio.write(genbarcode(content, width, height, srcimagepath),
     "jpg", new file(destimagepath));
  } catch (ioexception e) {
   e.printstacktrace();
  } catch (writerexception e) {
   e.printstacktrace();
  }
 }
 
 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);
  // 生成二维码 
  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 > 0 && x < 170 && y > 0 && y < 170) {
     color color = new color(231, 144, 56);
     int colorint = color.getrgb();
     pixels[y * width + x] = matrix.get(x, y) ? colorint
       : 16777215;
    }
    // 读取图片
    else 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 > halfw - 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 {
     // 二维码颜色
     int num1 = (int) (50 - (50.0 - 13.0) / matrix.getheight()
       * (y + 1));
     int num2 = (int) (165 - (165.0 - 72.0) / matrix.getheight()
       * (y + 1));
     int num3 = (int) (162 - (162.0 - 107.0)
       / matrix.getheight() * (y + 1));
     color color = new color(num1, num2, num3);
     int colorint = color.getrgb();
     // 此处可以修改二维码的颜色,可以分别制定二维码和背景的颜色; 
     pixels[y * width + x] = matrix.get(x, y) ? colorint
       : 16777215;
     // 0x000000:0xffffff  
    }
   }
  }
  bufferedimage image = new bufferedimage(width, height,
    bufferedimage.type_int_rgb);
  image.getraster().setdataelements(0, 0, width, height, pixels);
  return image;
 }
 
 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.white);
   graphic.fillrect(0, 0, 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);
   else
    graphic.drawimage(destimage,
      (width - destimage.getwidth(null)) / 2, 0,
      destimage.getwidth(null), destimage.getheight(null),
      color.white, null);
   graphic.dispose();
   destimage = image;
  }
  return (bufferedimage) destimage;
 }
 
 public static void main(string[] args) throws unsupportedencodingexception {
  // 依次为内容(不支持中文),宽,长,中间图标路径,储存路径 
  matrixtoimagewriter.encode("http://www.baidu.com/", 512, 512,
    "d:\\logo.png", "d:\\2013-01.jpg");
 
 }
}

大家可以参考专题:java二维码进行学习

以上就是本文的全部内容,帮助大家设计属于自己的二维码。