* 下载二维码图片
*
* @param request
* @param response
* @param qrcodeurl
* @throws IOException
*/
@RequestMapping(value = "todownpic")
public static void todownpic(HttpServletRequest request,
HttpServletResponse response, String qrcodeurl,String qrcodeId) throws IOException {
qrcodeurl = request.getParameter("qrcodeurl");
if (StringUtil.isNotEmpty(qrcodeurl)) {
BufferedImage ImageTwo = null;
try {
// 背景
File fileOne = new File(BASE);
BufferedImage ImageOne;
ImageOne = ImageIO.read(fileOne);
int width = ImageOne.getWidth();// 图片宽度
int height = ImageOne.getHeight();// 图片高度
// 从图片中读取RGB
int[] ImageArrayOne = new int[width * height];
ImageArrayOne = ImageOne.getRGB(0, 0, width, height,
ImageArrayOne, 0, width);
QrcodeUtil QrcodeUtil = new QrcodeUtil(150, 150);
ImageTwo = QrcodeUtil.genBarcode(qrcodeurl, width - 2 * x + 10,
width - 2 * x + 10, "D:\\logo\\logo.png");
int widthTwo = ImageTwo.getWidth();// 图片宽度
int heightTwo = ImageTwo.getHeight();// 图片高度
int[] ImageArrayTwo = new int[widthTwo * heightTwo];
ImageArrayTwo = ImageTwo.getRGB(0, 0, widthTwo, heightTwo,
ImageArrayTwo, 0, widthTwo);
// 生成新图片
BufferedImage ImageNew = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
ImageNew.setRGB(0, 0, width, height, ImageArrayOne, 0, width);// 设置左半部分的RGB
ImageNew.setRGB(x, y, widthTwo, heightTwo, ImageArrayTwo, 0,
widthTwo);// 设置右半部分的RGB
createMark(response, ImageNew, "NO." + qrcodeId, null, 1, "方正楷体简体", 30,
1150, 30, Color.WHITE);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void createMark(HttpServletResponse response,
BufferedImage ImageNew, String markContent, Color markContentColor,
float qualNum, String fontType, int fontSize, int w, int h,
Color color) {
markContentColor = color;
/* 构建要处理的源图片 */
ImageIcon imageIcon = new ImageIcon(ImageNew);
/* 获取要处理的图片 */
Image image = imageIcon.getImage();
// Image可以获得图片的属性信息
int width = image.getWidth(null);
int height = image.getHeight(null);
// 为画出与源图片的相同大小的图片(可以自己定义)
BufferedImage bImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 构建2D画笔
Graphics2D g = bImage.createGraphics();
/* 设置2D画笔的画出的文字颜色 */
g.setColor(markContentColor);
/* 设置2D画笔的画出的文字背景色 */
g.setBackground(Color.white);
/* 画出图片 */
g.drawImage(image, 0, 0, null);
/* --------对要显示的文字进行处理-------------- */
AttributedString ats = new AttributedString(markContent);
Font font = new Font(fontType, Font.BOLD, fontSize);
g.setFont(font);
/* 消除java.awt.Font字体的锯齿 */
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
/* 消除java.awt.Font字体的锯齿 */
// 更多实例请访问 http://www.walkerjava.com/forum.php?mod=viewthread&tid=135
// font = g.getFont().deriveFont(30.0f);
ats.addAttribute(TextAttribute.FONT, font, 0, markContent.length());
AttributedCharacterIterator iter = ats.getIterator();
/* 添加水印的文字和设置水印文字出现的内容 ----位置 */
g.drawString(iter, width - w, height - h);
/* --------对要显示的文字进行处理-------------- www.it165.net */
g.dispose();// 画笔结束
try {
// 输出 文件 到指定的路径
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(bImage, "jpg", imOut);
InputStream in = new ByteArrayInputStream(bs.toByteArray());
byte[] data = FileUtil.toByteArray(in);
String filename = StringUtil.getUUID().concat(".png");
response.reset();
response.setHeader("Content-Disposition", "attachment; filename=\""
+ filename + "\"");
response.addHeader("Content-Length", "" + data.length);
response.setContentType("application/octet-stream;charset=UTF-8");
OutputStream outputStream = new BufferedOutputStream(
response.getOutputStream());
outputStream.write(data);
outputStream.flush();
outputStream.close();
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 得到BufferedImage
*
* @param content
* 二维码显示的文本
* @param width
* 二维码的宽度
* @param height
* 二维码的高度
* @param srcImagePath
* 中间嵌套的图片
* @return
* @throws WriterException
* @throws IOException
*/
public static BufferedImage genBarcode(String content, int width, int height,
String srcImagePath) throws WriterException, IOException {
// 读取源图像
BufferedImage scaleImage = scale(srcImagePath, IMAGE_WIDTH,
IMAGE_HEIGHT, false);
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);
}
}
java.util.Hashtable hint = new java.util.Hashtable();
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;
}