1、多个图片合成
2、生成不带logo的二维码
3、读取图片二维码信息
4、生成带logo的二维码
5、图片压缩
6、文字水印到图片
7、图片水印到图片
9、解决图片红色问题 ,JDK中提供的Image
package com.hlj.util.QRcode;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
public class QrCodeUtils {
public static void saveImageToLocalDir(BufferedImage buffImg, String savePath) {
try {
ImageIO.write(buffImg, "jpg", new File(savePath));
} catch (IOException e1) {
e1.printStackTrace();
}
}
public static BufferedImage imageAndImages(BufferedImage exImage, BufferedImage innerImage, int x, int y, int innerImageWedith, int innerImageHeight, float alpha) throws IOException {
Graphics2D g2d = exImage.createGraphics();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g2d.drawImage(innerImage, x, y, innerImageWedith, innerImageHeight, null);
g2d.dispose();
return exImage;
}
@Test
public void testimageAndImages(){
String sourceFilePath = "/Users/healerjean/Desktop/origin.jpeg";
String innerImageFilePath = "/Users/healerjean/Desktop/img.jpeg";
BufferedImage buffImg = null;
try {
buffImg = imageAndImages(ImageIO.read(new File(sourceFilePath)), ImageIO.read(new File(innerImageFilePath)),238, 588,210 ,208, 1.0f);
} catch (IOException e) {
e.printStackTrace();
}
String saveFilePath = "/Users/healerjean/Desktop/new.png";
saveImageToLocalDir(buffImg, saveFilePath);
}
public static BufferedImage writeQRImg(String text,int width,int height,int whiteSize){
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.MARGIN, whiteSize);
BitMatrix bitMatrix = null;
try {
bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
} catch (WriterException e) {
e.printStackTrace();
}
int codeWidth = bitMatrix.getWidth();
int codeHeight = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < codeWidth; i++) {
for (int j = 0; j < codeHeight; j