怎么對(duì)網(wǎng)站做壓力測(cè)試seo排名優(yōu)化價(jià)格
今天干了一件特別不務(wù)正業(yè)的事,做了一個(gè)小程序用來(lái)給圖片添加水印。事情的起因是需要將自己的身份證照片分享給別人,手邊并沒(méi)有一個(gè)趁手的工具來(lái)生成圖片水印。很多APP提供了水印的功能,但會(huì)把我的圖片上傳到他們的服務(wù)器,身份證太敏感了,顯然我并不想讓別人有機(jī)會(huì)保留照片。
我把圖片處理做了一個(gè)抽象,入?yún)⑹荁ufferedImage,對(duì)圖片添加水印、盲印、隱式寫(xiě)入后返回新的BufferedImage作為結(jié)果。
package org.keyniu.watermark.image;import java.awt.image.BufferedImage;public interface ImageProcess {/*** @param org* @return*/public BufferedImage process(BufferedImage org) throws Exception;}
1. 基本實(shí)現(xiàn)
我們先給出一版基本的實(shí)現(xiàn)
package org.keyniu.watermark.image;...
/*** 基于JDK的Graphics2D實(shí)現(xiàn)*/
public class Graphics2DWatermark implements ImageProcess {... public BufferedImage process(BufferedImage org) throws UnsupportedEncodingException, NoSuchAlgorithmException {BufferedImage marked = new BufferedImage(org.getWidth(), org.getHeight(), BufferedImage.TYPE_INT_RGB);Graphics2D g2d = marked.createGraphics();g2d.drawImage(org, 0, 0, null); // 創(chuàng)建結(jié)果圖片,并繪制原圖// 設(shè)置字體,計(jì)算每個(gè)水印文字的塊大小FontRenderContext context = g2d.getFontRenderContext();Font font = new Font(fontName, Font.BOLD, fontSize);g2d.setFont(font);TextMetadata textMeta = getTextMetadata(font, context, text);// 設(shè)置水印透明度,默認(rèn)選擇45°g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha)); // 設(shè)置透明度,0.0~1.0g2d.rotate(Math.PI * rotateArch / 180, org.getWidth() / 2, org.getHeight() / 2);// 計(jì)算圖片中每行能放幾個(gè)水印,要放多少行ImageMetadata imageMeta = new ImageMetadata(org.getWidth(), org.getHeight());int columnCount = imageMeta.getColumnCount(textMeta.getWidth());int rowCount = imageMeta.getRowCount(textMeta.getHeight() + textMeta.getCrcHeight());AffineTransform transform = g2d.getTransform();for (int rIdx = 0; rIdx < rowCount; rIdx++) {for (int cIdx = 0; cIdx < columnCount; cIdx++) {g2d.setTransform(transform);randomRotate(g2d, imageMeta);randomTransform(g2d);watermark(g2d, imageMeta, textMeta, rIdx, cIdx);}}g2d.setTransform(transform);// 結(jié)束繪制,釋放資源g2d.dispose();return marked;}private void watermark(Graphics2D g2d, ImageMetadata imageMeta, TextMetadata textMeta, int rIdx, int cIdx) {Point offset = imageMeta.getOffset();Point textLoc = textMeta.textLocation(rIdx, cIdx);Point crcLoc = textMeta.crcLocation(rIdx, cIdx);randomGradient(g2d, offset.x + textLoc.x, offset.y + textLoc.y, textMeta.totalTextWidth(), textMeta.totalTextHeight());g2d.drawString(textMeta.getText(), offset.x + textLoc.x, offset.y + textLoc.y);randomGradient(g2d, offset.x + crcLoc.x, offset.y + crcLoc.y, textMeta.totalCrcWidth(), textMeta.totalCrcHeight());g2d.drawString(textMeta.getCrc(), offset.x + crcLoc.x, offset.y + crcLoc.y);}protected void randomRotate(Graphics2D g2d, ImageMetadata imageMeta) { // 供子類(lèi)覆蓋,自定義旋轉(zhuǎn)的邏輯}protected void randomTransform(Graphics2D g2d) { // 供子類(lèi)覆蓋,自定義AffineTransform的邏輯}protected void randomGradient(Graphics2D g2d, int x, int y, int dx, int dy) { // 供子類(lèi)覆蓋,實(shí)現(xiàn)漸變色的邏輯}...
}
本地main方法測(cè)試,測(cè)試代碼是這樣的的。
public static void main(String[] args) throws Exception {Graphics2DWatermark watermark = new Graphics2DWatermark("僅用于車(chē)險(xiǎn)辦理");BufferedImage image = ImageIO.read(new File("D:\\blog\\linux.png"));BufferedImage certified = watermark.process(image);ImageIO.write(certified, "jpg", new File("D:\\blog\\linux_mark.png"));
}
左邊是原始圖片,右邊是加了水印后的圖片
2. 旋轉(zhuǎn)變換
太有規(guī)律的水印很容易就被擦除水印,上面的實(shí)現(xiàn)中我們預(yù)留了3個(gè)接口,用來(lái)擴(kuò)展實(shí)現(xiàn),分別是:
- randomRotate,輸出一行水印之前,有機(jī)會(huì)做旋轉(zhuǎn)
- randomTransform,輸出一行水印前,有機(jī)會(huì)執(zhí)行AffineTransfrom
- randomGradient,輸出水印文字和CRC之前,有機(jī)會(huì)設(shè)置漸變色
我們提供了一個(gè)增強(qiáng)實(shí)現(xiàn)
public class EnhancedGraphics2DWatermark extends Graphics2DWatermark {public EnhancedGraphics2DWatermark(String text) {super(text);}protected void randomRotate(Graphics2D g2d, ImageMetadata imageMeta) {g2d.rotate(Math.PI * (Math.random() * 45 - 45) / 180, imageMeta.getSourceX(), imageMeta.getSourceY());}@Overrideprotected void randomTransform(Graphics2D g2d) {if (Math.random() < 0.5) {g2d.shear(Math.random() * 0.2, 0);} else {g2d.shear(0, Math.random() * 0.2);}}protected void randomGradient(Graphics2D g2d, int fx, int fy, int tx, int ty) {Color from = generateColor();Color to = reverse(from);GradientPaint gp = new GradientPaint(fx, fy, from, tx, ty, to);g2d.setPaint(gp);}private Color generateColor() {int r = (int) (256 * Math.random() + fontColor.getRed()) & 0xFF;int g = (int) (256 * Math.random() + fontColor.getGreen()) & 0xFF;int b = (int) (256 * Math.random() + fontColor.getBlue()) & 0xFF;return new Color(r, g, b);}private Color reverse(Color c) {return new Color((256 - c.getRed()) & 0xFF, (256 - c.getGreen()) & 0xFF, (256 - c.getBlue()) & 0XFF, c.getAlpha());}}
修改測(cè)試的main方法,改用這個(gè)實(shí)現(xiàn)
public static void main(String[] args) throws Exception {Graphics2DWatermark watermark = new EnhancedGraphics2DWatermark("僅用于車(chē)險(xiǎn)辦理");BufferedImage image = ImageIO.read(new File("D:\\blog\\linux.png"));BufferedImage certified = watermark.process(image);ImageIO.write(certified, "jpg", new File("D:\\blog\\linux_mark.png"));
}
這是新的水印效果
3. 提供GUI訪(fǎng)問(wèn)
直接通過(guò)代碼來(lái)調(diào)用對(duì)非程序來(lái)說(shuō)太有友好了,所以我在上一篇的基礎(chǔ)上做了一點(diǎn)點(diǎn)改成,做了一個(gè)GUI入口,通過(guò)菜單設(shè)置水印的文案
然后再使用JFileChooser打開(kāi)一個(gè)圖片文件,最終展示水印后的圖片。
完整的項(xiàng)目代碼見(jiàn)附件,如果使用GraalVM打包稱(chēng)為可執(zhí)行文件,就可以分享給你的小伙伴們使用啦。