|
|
|
|
@ -1,393 +1,393 @@
|
|
|
|
|
/* */ package com.archive.common.convert;
|
|
|
|
|
package com.archive.common.convert;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* */
|
|
|
|
|
/* */ import com.lowagie.text.Document;
|
|
|
|
|
/* */ import com.lowagie.text.Element;
|
|
|
|
|
/* */ import com.lowagie.text.Image;
|
|
|
|
|
/* */ import com.lowagie.text.Rectangle;
|
|
|
|
|
/* */ import com.lowagie.text.pdf.PdfWriter;
|
|
|
|
|
/* */ import java.awt.AlphaComposite;
|
|
|
|
|
/* */ import java.awt.Color;
|
|
|
|
|
/* */ import java.awt.Font;
|
|
|
|
|
/* */ import java.awt.Graphics;
|
|
|
|
|
/* */ import java.awt.Graphics2D;
|
|
|
|
|
/* */
|
|
|
|
|
/* */ import java.awt.RenderingHints;
|
|
|
|
|
/* */ import java.awt.image.BufferedImage;
|
|
|
|
|
/* */ import java.io.File;
|
|
|
|
|
/* */ import java.io.FileOutputStream;
|
|
|
|
|
/* */ import java.io.IOException;
|
|
|
|
|
/* */ import java.text.DecimalFormat;
|
|
|
|
|
/* */ import java.util.ArrayList;
|
|
|
|
|
/* */ import java.util.Arrays;
|
|
|
|
|
/* */ import javax.imageio.ImageIO;
|
|
|
|
|
/* */ import javax.swing.ImageIcon;
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public class ImgConvert
|
|
|
|
|
/* */ {
|
|
|
|
|
/* */ public static void ImagesToPdf(String imageFolderPath, String pdfPath) {
|
|
|
|
|
/* */ try {
|
|
|
|
|
/* 29 */ long start = System.currentTimeMillis();
|
|
|
|
|
/* 30 */ ArrayList<String> list = new ArrayList<>();
|
|
|
|
|
/* */
|
|
|
|
|
/* 32 */ String imagePath = null;
|
|
|
|
|
/* 33 */ FileOutputStream fos = new FileOutputStream(pdfPath);
|
|
|
|
|
/* */
|
|
|
|
|
/* 35 */ Document doc = new Document(null, 20.0F, 20.0F, 20.0F, 20.0F);
|
|
|
|
|
/* */
|
|
|
|
|
/* 37 */ PdfWriter.getInstance(doc, fos);
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 43 */ File file = new File(imageFolderPath);
|
|
|
|
|
/* 44 */ File[] files = file.listFiles();
|
|
|
|
|
/* */
|
|
|
|
|
/* 46 */ for (File file1 : files) {
|
|
|
|
|
/* 47 */ if (file1.getName().endsWith(".png") || file1.getName().endsWith(".jpg") || file1.getName().endsWith(".gif") || file1
|
|
|
|
|
/* 48 */ .getName().endsWith(".jpeg") || file1.getName().endsWith(".tif")) {
|
|
|
|
|
/* 49 */ imagePath = imageFolderPath + File.separator + file1.getName();
|
|
|
|
|
/* 50 */ list.add(file1.getName());
|
|
|
|
|
/* */
|
|
|
|
|
/* 52 */ BufferedImage img = ImageIO.read(new File(imagePath));
|
|
|
|
|
/* */
|
|
|
|
|
/* 54 */ doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
|
|
|
|
|
/* */
|
|
|
|
|
/* 56 */ Image image = Image.getInstance(imagePath);
|
|
|
|
|
/* */
|
|
|
|
|
/* 58 */ doc.open();
|
|
|
|
|
/* 59 */ doc.add((Element)image);
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 64 */ doc.close();
|
|
|
|
|
/* 65 */ long endTime = System.currentTimeMillis();
|
|
|
|
|
/* 66 */ int time = (int)((endTime - start) / 1000L);
|
|
|
|
|
/* 67 */ System.out.println("用时:{" + time + "}:秒!");
|
|
|
|
|
/* 68 */ } catch (Exception e) {
|
|
|
|
|
/* 69 */ e.printStackTrace();
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static void ImgToPdf(String imagePath, String pdfPath) {
|
|
|
|
|
/* */ try {
|
|
|
|
|
/* 81 */ long start = System.currentTimeMillis();
|
|
|
|
|
/* 82 */ ArrayList<String> list = new ArrayList<>();
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 85 */ FileOutputStream fos = new FileOutputStream(pdfPath);
|
|
|
|
|
/* */
|
|
|
|
|
/* 87 */ Document doc = new Document(null, 20.0F, 20.0F, 20.0F, 20.0F);
|
|
|
|
|
/* */
|
|
|
|
|
/* 89 */ PdfWriter.getInstance(doc, fos);
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 95 */ BufferedImage img = ImageIO.read(new File(imagePath));
|
|
|
|
|
/* */
|
|
|
|
|
/* 97 */ doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
|
|
|
|
|
/* */
|
|
|
|
|
/* 99 */ Image image = Image.getInstance(imagePath);
|
|
|
|
|
/* */
|
|
|
|
|
/* 101 */ doc.open();
|
|
|
|
|
/* 102 */ doc.add((Element)image);
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 106 */ doc.close();
|
|
|
|
|
/* 107 */ long endTime = System.currentTimeMillis();
|
|
|
|
|
/* 108 */ int time = (int)((endTime - start) / 1000L);
|
|
|
|
|
/* 109 */ System.out.println("用时:{" + time + "}:秒!");
|
|
|
|
|
/* 110 */ } catch (Exception e) {
|
|
|
|
|
/* 111 */ e.printStackTrace();
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static void PngToJpg(String pngPath, String jpgPath) {
|
|
|
|
|
/* */ try {
|
|
|
|
|
/* 125 */ BufferedImage bufferedImage = ImageIO.read(new File(pngPath));
|
|
|
|
|
/* 126 */ BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), 1);
|
|
|
|
|
/* 127 */ newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.white, null);
|
|
|
|
|
/* 128 */ ImageIO.write(newBufferedImage, "jpg", new File(jpgPath));
|
|
|
|
|
/* 129 */ } catch (Exception e) {
|
|
|
|
|
/* 130 */ System.out.println("ERROR:png转jpg出现异常:" + e.getMessage());
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static void JpgToPng(String jpgPath, String pngPath) {
|
|
|
|
|
/* */ try {
|
|
|
|
|
/* 144 */ BufferedImage bufferedImage = ImageIO.read(new File(jpgPath));
|
|
|
|
|
/* 145 */ BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), 1);
|
|
|
|
|
/* 146 */ newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.white, null);
|
|
|
|
|
/* 147 */ ImageIO.write(newBufferedImage, "png", new File(pngPath));
|
|
|
|
|
/* 148 */ } catch (Exception e) {
|
|
|
|
|
/* 149 */ System.out.println("ERROR:jpg转png出现异常:" + e.getMessage());
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static BufferedImage waterMarkByText(int width, int heigth, String text, Color color, Font font, Double degree, float alpha) {
|
|
|
|
|
/* 167 */ BufferedImage buffImg = new BufferedImage(width, heigth, 1);
|
|
|
|
|
/* */
|
|
|
|
|
/* 169 */ Graphics2D g2d = buffImg.createGraphics();
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 172 */ buffImg = g2d.getDeviceConfiguration().createCompatibleImage(width, heigth, 3);
|
|
|
|
|
/* 173 */ g2d.dispose();
|
|
|
|
|
/* 174 */ g2d = buffImg.createGraphics();
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 178 */ g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 186 */ if (null != degree)
|
|
|
|
|
/* */ {
|
|
|
|
|
/* */
|
|
|
|
|
/* 189 */ g2d.rotate(Math.toRadians(degree.doubleValue()), buffImg.getWidth() / 2.0D, buffImg
|
|
|
|
|
/* 190 */ .getHeight() / 2.0D);
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 194 */ g2d.setColor(color);
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 197 */ g2d.setFont(font);
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 200 */ g2d.setComposite(AlphaComposite.getInstance(3, alpha));
|
|
|
|
|
/* */
|
|
|
|
|
/* 202 */ float realWidth = getRealFontWidth(text);
|
|
|
|
|
/* 203 */ float fontSize = font.getSize();
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* 206 */ float x = 0.5F * width - 0.5F * fontSize * realWidth;
|
|
|
|
|
/* 207 */ float y = 0.5F * heigth + 0.5F * fontSize;
|
|
|
|
|
/* */
|
|
|
|
|
/* 209 */ g2d.drawString(text, x, y);
|
|
|
|
|
/* */
|
|
|
|
|
/* 211 */ g2d.dispose();
|
|
|
|
|
/* */
|
|
|
|
|
/* 213 */ return buffImg;
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static BufferedImage waterMarkByText(int width, int heigth, String text, Color color, float alpha) {
|
|
|
|
|
/* 219 */ Font font = new Font("Dialog", 0, 33);
|
|
|
|
|
/* 220 */ return waterMarkByText(width, heigth, text, color, font, Double.valueOf(-30.0D), alpha);
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static BufferedImage waterMarkByText(int width, int heigth, String text, float alpha) {
|
|
|
|
|
/* 224 */ return waterMarkByText(width, heigth, text, Color.GRAY, alpha);
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static BufferedImage waterMarkByText(int width, int heigth, String text) {
|
|
|
|
|
/* 229 */ return waterMarkByText(width, heigth, text, 0.2F);
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static BufferedImage waterMarkByText(String text) {
|
|
|
|
|
/* 233 */ return waterMarkByText(200, 150, text);
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */ private static float getRealFontWidth(String text) {
|
|
|
|
|
/* 240 */ int len = text.length();
|
|
|
|
|
/* 241 */ float width = 0.0F;
|
|
|
|
|
/* 242 */ for (int i = 0; i < len; i++) {
|
|
|
|
|
/* 243 */ if (text.charAt(i) < 'Ā') {
|
|
|
|
|
/* 244 */ width += 0.5F;
|
|
|
|
|
/* */ } else {
|
|
|
|
|
/* 246 */ width++;
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ }
|
|
|
|
|
/* 249 */ return width;
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static void changeImgColor(String path, String newPath) throws IOException {
|
|
|
|
|
/* 263 */ File file = new File(path);
|
|
|
|
|
/* 264 */ BufferedImage bi = ImageIO.read(file);
|
|
|
|
|
/* 265 */ java.awt.Image image = bi;
|
|
|
|
|
/* */
|
|
|
|
|
/* 267 */ ImageIcon imageIcon = new ImageIcon(image);
|
|
|
|
|
/* 268 */ int width = imageIcon.getIconWidth();
|
|
|
|
|
/* 269 */ int height = imageIcon.getIconHeight();
|
|
|
|
|
/* */
|
|
|
|
|
/* 271 */ BufferedImage bufferedImage = new BufferedImage(width, height, 6);
|
|
|
|
|
/* 272 */ Graphics2D graphics2D = (Graphics2D)bufferedImage.getGraphics();
|
|
|
|
|
/* 273 */ graphics2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
|
|
|
|
|
/* 274 */ int alpha = 255;
|
|
|
|
|
/* */
|
|
|
|
|
/* 276 */ int RGB = bufferedImage.getRGB(width - 1, height - 1);
|
|
|
|
|
/* 277 */ for (int i = bufferedImage.getMinX(); i < width; i++) {
|
|
|
|
|
/* 278 */ for (int j = bufferedImage.getMinY(); j < height; j++) {
|
|
|
|
|
/* 279 */ int rgb = bufferedImage.getRGB(i, j);
|
|
|
|
|
/* 280 */ int r = (rgb & 0xFF0000) >> 16;
|
|
|
|
|
/* 281 */ int g = (rgb & 0xFF00) >> 8;
|
|
|
|
|
/* 282 */ int b = rgb & 0xFF;
|
|
|
|
|
/* 283 */ int R = (RGB & 0xFF0000) >> 16;
|
|
|
|
|
/* 284 */ int G = (RGB & 0xFF00) >> 8;
|
|
|
|
|
/* 285 */ int B = RGB & 0xFF;
|
|
|
|
|
/* */
|
|
|
|
|
/* 287 */ int a = 45;
|
|
|
|
|
/* 288 */ if (Math.abs(R - r) < a && Math.abs(G - g) < a && Math.abs(B - b) < a) {
|
|
|
|
|
/* 289 */ alpha = 0;
|
|
|
|
|
/* */ } else {
|
|
|
|
|
/* 291 */ alpha = 255;
|
|
|
|
|
/* */ }
|
|
|
|
|
/* 293 */ rgb = alpha << 24 | rgb & 0xFFFFFF;
|
|
|
|
|
/* 294 */ bufferedImage.setRGB(i, j, rgb);
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ }
|
|
|
|
|
/* 297 */ ImageIO.write(bufferedImage, "png", new File(newPath));
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static String convertRgbStr(int color) {
|
|
|
|
|
/* 301 */ int red = (color & 0xFF0000) >> 16;
|
|
|
|
|
/* 302 */ int green = (color & 0xFF00) >> 8;
|
|
|
|
|
/* 303 */ int blue = color & 0xFF;
|
|
|
|
|
/* 304 */ return red + "," + green + "," + blue;
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static String thumbnailImage(String imagePath, int w, int h, String prevfix, boolean force) {
|
|
|
|
|
/* 317 */ File imgFile = new File(imagePath);
|
|
|
|
|
/* 318 */ String newPath = "";
|
|
|
|
|
/* 319 */ if (imgFile.exists()) {
|
|
|
|
|
/* */
|
|
|
|
|
/* */ try {
|
|
|
|
|
/* 322 */ String types = Arrays.toString((Object[])ImageIO.getReaderFormatNames());
|
|
|
|
|
/* 323 */ String suffix = null;
|
|
|
|
|
/* */
|
|
|
|
|
/* 325 */ if (imgFile.getName().indexOf(".") > -1) {
|
|
|
|
|
/* 326 */ suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".") + 1);
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* 329 */ if (suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase()) < 0) {
|
|
|
|
|
/* 330 */ System.out.println("Sorry, the image suffix is illegal. the standard image suffix is {}." + types);
|
|
|
|
|
/* 331 */ return newPath;
|
|
|
|
|
/* */ }
|
|
|
|
|
/* 333 */ System.out.println("target image's size, width:{" + w + "}, height:{" + h + "}.");
|
|
|
|
|
/* 334 */ java.awt.Image img = ImageIO.read(imgFile);
|
|
|
|
|
/* 335 */ if (!force) {
|
|
|
|
|
/* */
|
|
|
|
|
/* 337 */ int width = img.getWidth(null);
|
|
|
|
|
/* 338 */ int height = img.getHeight(null);
|
|
|
|
|
/* 339 */ if (width * 1.0D / w < height * 1.0D / h) {
|
|
|
|
|
/* 340 */ if (width > w) {
|
|
|
|
|
/* 341 */ h = Integer.parseInt((new DecimalFormat("0")).format((height * w) / width * 1.0D));
|
|
|
|
|
/* 342 */ System.out.println("change image's height, width:{" + w + "}, height:{" + h + "}.");
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* 345 */ } else if (height > h) {
|
|
|
|
|
/* 346 */ w = Integer.parseInt((new DecimalFormat("0")).format((width * h) / height * 1.0D));
|
|
|
|
|
/* 347 */ System.out.println("change image's width, width:{" + w + "}, height:{" + h + "}.");
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* 351 */ BufferedImage bi = new BufferedImage(w, h, 1);
|
|
|
|
|
/* 352 */ Graphics g = bi.getGraphics();
|
|
|
|
|
/* 353 */ g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
|
|
|
|
|
/* 354 */ g.dispose();
|
|
|
|
|
/* 355 */ String p = imgFile.getPath();
|
|
|
|
|
/* */
|
|
|
|
|
/* 357 */ newPath = p.substring(0, p.lastIndexOf(File.separator)) + File.separator + prevfix + imgFile.getName();
|
|
|
|
|
/* 358 */ ImageIO.write(bi, suffix, new File(newPath));
|
|
|
|
|
/* 359 */ System.out.println("缩略图在原路径下生成成功");
|
|
|
|
|
/* 360 */ } catch (IOException e) {
|
|
|
|
|
/* 361 */ System.out.println("generate thumbnail image failed." + e);
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ } else {
|
|
|
|
|
/* 364 */ System.out.println("the image is not exist.");
|
|
|
|
|
/* */ }
|
|
|
|
|
/* 366 */ return newPath;
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */ public static void main(String[] args) {
|
|
|
|
|
/* */ try {
|
|
|
|
|
/* 383 */ changeImgColor("F:\\FFOutput\\profile.png", "F:\\FFOutput\\profile1.png");
|
|
|
|
|
/* 384 */ } catch (IOException e) {
|
|
|
|
|
/* 385 */ e.printStackTrace();
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ }
|
|
|
|
|
/* */ }
|
|
|
|
|
|
|
|
|
|
import com.lowagie.text.Document;
|
|
|
|
|
import com.lowagie.text.Element;
|
|
|
|
|
import com.lowagie.text.Image;
|
|
|
|
|
import com.lowagie.text.Rectangle;
|
|
|
|
|
import com.lowagie.text.pdf.PdfWriter;
|
|
|
|
|
import java.awt.AlphaComposite;
|
|
|
|
|
import java.awt.Color;
|
|
|
|
|
import java.awt.Font;
|
|
|
|
|
import java.awt.Graphics;
|
|
|
|
|
import java.awt.Graphics2D;
|
|
|
|
|
|
|
|
|
|
import java.awt.RenderingHints;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.text.DecimalFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import javax.swing.ImageIcon;
|
|
|
|
|
|
|
|
|
|
public class ImgConvert
|
|
|
|
|
{
|
|
|
|
|
public static void ImagesToPdf(String imageFolderPath, String pdfPath) {
|
|
|
|
|
try {
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
ArrayList<String> list = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
String imagePath = null;
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(pdfPath);
|
|
|
|
|
|
|
|
|
|
Document doc = new Document(null, 20.0F, 20.0F, 20.0F, 20.0F);
|
|
|
|
|
|
|
|
|
|
PdfWriter.getInstance(doc, fos);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
File file = new File(imageFolderPath);
|
|
|
|
|
File[] files = file.listFiles();
|
|
|
|
|
|
|
|
|
|
for (File file1 : files) {
|
|
|
|
|
if (file1.getName().endsWith(".png") || file1.getName().endsWith(".jpg") || file1.getName().endsWith(".gif") || file1
|
|
|
|
|
.getName().endsWith(".jpeg") || file1.getName().endsWith(".tif")) {
|
|
|
|
|
imagePath = imageFolderPath + File.separator + file1.getName();
|
|
|
|
|
list.add(file1.getName());
|
|
|
|
|
|
|
|
|
|
BufferedImage img = ImageIO.read(new File(imagePath));
|
|
|
|
|
|
|
|
|
|
doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
|
|
|
|
|
|
|
|
|
|
Image image = Image.getInstance(imagePath);
|
|
|
|
|
|
|
|
|
|
doc.open();
|
|
|
|
|
doc.add((Element)image);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
doc.close();
|
|
|
|
|
long endTime = System.currentTimeMillis();
|
|
|
|
|
int time = (int)((endTime - start) / 1000L);
|
|
|
|
|
System.out.println("用时:{" + time + "}:秒!");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void ImgToPdf(String imagePath, String pdfPath) {
|
|
|
|
|
try {
|
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
ArrayList<String> list = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
FileOutputStream fos = new FileOutputStream(pdfPath);
|
|
|
|
|
|
|
|
|
|
Document doc = new Document(null, 20.0F, 20.0F, 20.0F, 20.0F);
|
|
|
|
|
|
|
|
|
|
PdfWriter.getInstance(doc, fos);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
BufferedImage img = ImageIO.read(new File(imagePath));
|
|
|
|
|
|
|
|
|
|
doc.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));
|
|
|
|
|
|
|
|
|
|
Image image = Image.getInstance(imagePath);
|
|
|
|
|
|
|
|
|
|
doc.open();
|
|
|
|
|
doc.add((Element)image);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
doc.close();
|
|
|
|
|
long endTime = System.currentTimeMillis();
|
|
|
|
|
int time = (int)((endTime - start) / 1000L);
|
|
|
|
|
System.out.println("用时:{" + time + "}:秒!");
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void PngToJpg(String pngPath, String jpgPath) {
|
|
|
|
|
try {
|
|
|
|
|
BufferedImage bufferedImage = ImageIO.read(new File(pngPath));
|
|
|
|
|
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), 1);
|
|
|
|
|
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.white, null);
|
|
|
|
|
ImageIO.write(newBufferedImage, "jpg", new File(jpgPath));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.out.println("ERROR:png转jpg出现异常:" + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void JpgToPng(String jpgPath, String pngPath) {
|
|
|
|
|
try {
|
|
|
|
|
BufferedImage bufferedImage = ImageIO.read(new File(jpgPath));
|
|
|
|
|
BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), 1);
|
|
|
|
|
newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, Color.white, null);
|
|
|
|
|
ImageIO.write(newBufferedImage, "png", new File(pngPath));
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
System.out.println("ERROR:jpg转png出现异常:" + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static BufferedImage waterMarkByText(int width, int heigth, String text, Color color, Font font, Double degree, float alpha) {
|
|
|
|
|
BufferedImage buffImg = new BufferedImage(width, heigth, 1);
|
|
|
|
|
|
|
|
|
|
Graphics2D g2d = buffImg.createGraphics();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
buffImg = g2d.getDeviceConfiguration().createCompatibleImage(width, heigth, 3);
|
|
|
|
|
g2d.dispose();
|
|
|
|
|
g2d = buffImg.createGraphics();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (null != degree)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
g2d.rotate(Math.toRadians(degree.doubleValue()), buffImg.getWidth() / 2.0D, buffImg
|
|
|
|
|
.getHeight() / 2.0D);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
g2d.setColor(color);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
g2d.setFont(font);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
g2d.setComposite(AlphaComposite.getInstance(3, alpha));
|
|
|
|
|
|
|
|
|
|
float realWidth = getRealFontWidth(text);
|
|
|
|
|
float fontSize = font.getSize();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
float x = 0.5F * width - 0.5F * fontSize * realWidth;
|
|
|
|
|
float y = 0.5F * heigth + 0.5F * fontSize;
|
|
|
|
|
|
|
|
|
|
g2d.drawString(text, x, y);
|
|
|
|
|
|
|
|
|
|
g2d.dispose();
|
|
|
|
|
|
|
|
|
|
return buffImg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static BufferedImage waterMarkByText(int width, int heigth, String text, Color color, float alpha) {
|
|
|
|
|
Font font = new Font("Dialog", 0, 33);
|
|
|
|
|
return waterMarkByText(width, heigth, text, color, font, Double.valueOf(-30.0D), alpha);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static BufferedImage waterMarkByText(int width, int heigth, String text, float alpha) {
|
|
|
|
|
return waterMarkByText(width, heigth, text, Color.GRAY, alpha);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static BufferedImage waterMarkByText(int width, int heigth, String text) {
|
|
|
|
|
return waterMarkByText(width, heigth, text, 0.2F);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static BufferedImage waterMarkByText(String text) {
|
|
|
|
|
return waterMarkByText(200, 150, text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static float getRealFontWidth(String text) {
|
|
|
|
|
int len = text.length();
|
|
|
|
|
float width = 0.0F;
|
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
|
if (text.charAt(i) < 'Ā') {
|
|
|
|
|
width += 0.5F;
|
|
|
|
|
} else {
|
|
|
|
|
width++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return width;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void changeImgColor(String path, String newPath) throws IOException {
|
|
|
|
|
File file = new File(path);
|
|
|
|
|
BufferedImage bi = ImageIO.read(file);
|
|
|
|
|
java.awt.Image image = bi;
|
|
|
|
|
|
|
|
|
|
ImageIcon imageIcon = new ImageIcon(image);
|
|
|
|
|
int width = imageIcon.getIconWidth();
|
|
|
|
|
int height = imageIcon.getIconHeight();
|
|
|
|
|
|
|
|
|
|
BufferedImage bufferedImage = new BufferedImage(width, height, 6);
|
|
|
|
|
Graphics2D graphics2D = (Graphics2D)bufferedImage.getGraphics();
|
|
|
|
|
graphics2D.drawImage(imageIcon.getImage(), 0, 0, imageIcon.getImageObserver());
|
|
|
|
|
int alpha = 255;
|
|
|
|
|
|
|
|
|
|
int RGB = bufferedImage.getRGB(width - 1, height - 1);
|
|
|
|
|
for (int i = bufferedImage.getMinX(); i < width; i++) {
|
|
|
|
|
for (int j = bufferedImage.getMinY(); j < height; j++) {
|
|
|
|
|
int rgb = bufferedImage.getRGB(i, j);
|
|
|
|
|
int r = (rgb & 0xFF0000) >> 16;
|
|
|
|
|
int g = (rgb & 0xFF00) >> 8;
|
|
|
|
|
int b = rgb & 0xFF;
|
|
|
|
|
int R = (RGB & 0xFF0000) >> 16;
|
|
|
|
|
int G = (RGB & 0xFF00) >> 8;
|
|
|
|
|
int B = RGB & 0xFF;
|
|
|
|
|
|
|
|
|
|
int a = 45;
|
|
|
|
|
if (Math.abs(R - r) < a && Math.abs(G - g) < a && Math.abs(B - b) < a) {
|
|
|
|
|
alpha = 0;
|
|
|
|
|
} else {
|
|
|
|
|
alpha = 255;
|
|
|
|
|
}
|
|
|
|
|
rgb = alpha << 24 | rgb & 0xFFFFFF;
|
|
|
|
|
bufferedImage.setRGB(i, j, rgb);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ImageIO.write(bufferedImage, "png", new File(newPath));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String convertRgbStr(int color) {
|
|
|
|
|
int red = (color & 0xFF0000) >> 16;
|
|
|
|
|
int green = (color & 0xFF00) >> 8;
|
|
|
|
|
int blue = color & 0xFF;
|
|
|
|
|
return red + "," + green + "," + blue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static String thumbnailImage(String imagePath, int w, int h, String prevfix, boolean force) {
|
|
|
|
|
File imgFile = new File(imagePath);
|
|
|
|
|
String newPath = "";
|
|
|
|
|
if (imgFile.exists()) {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
String types = Arrays.toString((Object[])ImageIO.getReaderFormatNames());
|
|
|
|
|
String suffix = null;
|
|
|
|
|
|
|
|
|
|
if (imgFile.getName().indexOf(".") > -1) {
|
|
|
|
|
suffix = imgFile.getName().substring(imgFile.getName().lastIndexOf(".") + 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (suffix == null || types.toLowerCase().indexOf(suffix.toLowerCase()) < 0) {
|
|
|
|
|
System.out.println("Sorry, the image suffix is illegal. the standard image suffix is {}." + types);
|
|
|
|
|
return newPath;
|
|
|
|
|
}
|
|
|
|
|
System.out.println("target image's size, width:{" + w + "}, height:{" + h + "}.");
|
|
|
|
|
java.awt.Image img = ImageIO.read(imgFile);
|
|
|
|
|
if (!force) {
|
|
|
|
|
|
|
|
|
|
int width = img.getWidth(null);
|
|
|
|
|
int height = img.getHeight(null);
|
|
|
|
|
if (width * 1.0D / w < height * 1.0D / h) {
|
|
|
|
|
if (width > w) {
|
|
|
|
|
h = Integer.parseInt((new DecimalFormat("0")).format((height * w) / width * 1.0D));
|
|
|
|
|
System.out.println("change image's height, width:{" + w + "}, height:{" + h + "}.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (height > h) {
|
|
|
|
|
w = Integer.parseInt((new DecimalFormat("0")).format((width * h) / height * 1.0D));
|
|
|
|
|
System.out.println("change image's width, width:{" + w + "}, height:{" + h + "}.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BufferedImage bi = new BufferedImage(w, h, 1);
|
|
|
|
|
Graphics g = bi.getGraphics();
|
|
|
|
|
g.drawImage(img, 0, 0, w, h, Color.LIGHT_GRAY, null);
|
|
|
|
|
g.dispose();
|
|
|
|
|
String p = imgFile.getPath();
|
|
|
|
|
|
|
|
|
|
newPath = p.substring(0, p.lastIndexOf(File.separator)) + File.separator + prevfix + imgFile.getName();
|
|
|
|
|
ImageIO.write(bi, suffix, new File(newPath));
|
|
|
|
|
System.out.println("缩略图在原路径下生成成功");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
System.out.println("generate thumbnail image failed." + e);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
System.out.println("the image is not exist.");
|
|
|
|
|
}
|
|
|
|
|
return newPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
try {
|
|
|
|
|
changeImgColor("F:\\FFOutput\\profile.png", "F:\\FFOutput\\profile1.png");
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Location: C:\Users\Administrator\Desktop\extracted.zip!\extracted\BOOT-INF\classes\com\archive\common\convert\ImgConvert.class
|
|
|
|
|
|