/**
* Created by Amber Wang on 2017/11/27 17:25.
*/
import java.awt.*;
public class LogoConfig {
public static final Color DEFAULT_BORDERCOLOR = Color.WHITE;
public static final int DEFAULT_BORDER = 2;
public static final int DEFAULT_LOGOPART = 6;
private final int border = DEFAULT_BORDER;
private final Color borderColor;
private final int logoPart;
/**
* Creates a default config with on color {@link #BLACK} and off color
* {@link #WHITE}, generating normal black-on-white barcodes.
*/
public LogoConfig() {
this(DEFAULT_BORDERCOLOR, DEFAULT_LOGOPART);
}
public LogoConfig(Color borderColor, int logoPart) {
this.borderColor = borderColor;
this.logoPart = logoPart;
}
public Color getBorderColor() {
return borderColor;
}
public int getBorder() {
return border;
}
public int getLogoPart() {
return logoPart;
}
}
/**
* Created by Amber Wang on 2017/11/27 17:24.
*/
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
public final class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {
}
public static 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 - 50; y++) {
image.setRGB(x, y, matrix.get(x, y + 50) ? BLACK : WHITE);
}
}
return image;
}
public static BufferedImage writeToFile(BitMatrix matrix, String format)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
return image;
}
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
}
/**
* 给二维码图片添加Logo
*
* @param barCodeImage
* @param logoPic
*/
public static BufferedImage addLogo_QRCode(BufferedImage barCodeImage, File logoPic, LogoConfig logoConfig) {
try {
if (!logoPic.isFile()) {
System.out.print("file not find !");
throw new IOException("file not find !");
}
/**
* 读取二维码图片,并构建绘图对象
*/
Graphics2D g = barCodeImage.createGraphics();
/**
* 读取Logo图片
*/
BufferedImage logo = ImageIO.read(logoPic);
int widthLogo = barCodeImage.getWidth() / logoConfig.getLogoPart();
int heightLogo = barCodeImage.getWidth() / logoConfig.getLogoPart();
int x = (barCodeImage.getWidth() - widthLogo) / 2;
int y = (barCodeImage.getHeight() - heightLogo) / 2 - 50;
g.drawImage(logo, x, y, widthLogo, heightLogo, null);
g.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10);
g.setStroke(new BasicStroke(logoConfig.getBorder()));
g.setColor(logoConfig.getBorderColor());
g.drawRect(x, y, widthLogo, heightLogo);
g.dispose();
return barCodeImage;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param pressText 文字
* @param newImg 带文字的图片
* @param image 需要添加文字的图片
* @param fontStyle
* @param color
* @param fontSize
* @param width
* @param height
* @为图片添加文字
*/
public static void pressText(String pressText, String newImg, BufferedImage image, int fontStyle, Color color, int fontSize, int width, int height) {
int startX = (width - (fontSize * pressText.length())) - 10;
int startY = height - (height - width) / 2 - 30;
System.out.println("startX: " + startX);
System.out.println("startY: " + startY);
System.out.println("height: " + height);
System.out.println("width: " + width);
System.out.println("fontSize: " + fontSize);
System.out.println("pressText.length(): " + pressText.length());
try {
int imageW = image.getWidth();
int imageH = image.getHeight();
Graphics g = image.createGraphics();
g.drawImage(image, 0, 0, imageW, imageH, null);
g.setColor(color);
g.setFont(new Font("粗体", Font.BOLD, 30));
g.drawString(pressText, startX, startY);
g.drawString("广 告 牌 0 1", startX + 30, startY + 30);
g.dispose();
FileOutputStream out = new FileOutputStream(newImg);
ImageIO.write(image, "JPEG", out);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
System.out.println("image press success");
} catch (Exception e) {
e.printStackTrace();
System.out.println(e);
}
}
private static String content = "http://www.changxingnanjing.com?qr_code=01900302|773f3c8f872bf34cc3fe97022720330e";
private static String path = "E:/QRCodeImage";
public static void main(String args[]) {
try {
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
@SuppressWarnings("rawtypes")
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.MARGIN, 2);
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
int width = 352;
int height = 500;
BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
File logoFile = new File(path, "logo.jpg");
BufferedImage barCodeImage = MatrixToImageWriter.writeToFile(bitMatrix, "jpg");
LogoConfig logoConfig = new LogoConfig();
BufferedImage image = addLogo_QRCode(barCodeImage, logoFile, logoConfig);
int font = 18;
int fontStyle = 1;
String newImageWithText = path + "/imageWithText.jpg";
String text = "0 1 1 0 9 0 2 9";
pressText(text, newImageWithText, image, fontStyle, Color.black, font, width, height);
} catch (Exception e) {
e.printStackTrace();
}
}