|
| 1 | +package org.metanorma.fop.utils; |
| 2 | + |
| 3 | +import org.metanorma.utils.LoggerHelper; |
| 4 | +import org.w3c.dom.NodeList; |
| 5 | + |
| 6 | +import javax.imageio.ImageIO; |
| 7 | +import javax.imageio.ImageReader; |
| 8 | +import javax.imageio.metadata.IIOMetadata; |
| 9 | +import javax.imageio.metadata.IIOMetadataFormatImpl; |
| 10 | +import javax.imageio.metadata.IIOMetadataNode; |
| 11 | +import javax.imageio.stream.ImageInputStream; |
| 12 | +import java.awt.image.BufferedImage; |
| 13 | +import java.io.ByteArrayInputStream; |
| 14 | +import java.io.ByteArrayOutputStream; |
| 15 | +import java.io.File; |
| 16 | +import java.util.Base64; |
| 17 | +import java.util.Iterator; |
| 18 | +import java.util.logging.Level; |
| 19 | +import java.util.logging.Logger; |
| 20 | + |
| 21 | +/** |
| 22 | + * |
| 23 | + * @author Alexander Dyuzhev |
| 24 | + */ |
| 25 | +public class ImageUtils { |
| 26 | + protected static final Logger logger = Logger.getLogger(LoggerHelper.LOGGER_NAME); |
| 27 | + |
| 28 | + public static String getImageScale(String img, String width_effective, String height_effective) { |
| 29 | + try { |
| 30 | + BufferedImage bufferedImage; |
| 31 | + ImageInputStream imageInputStream; |
| 32 | + if (!img.startsWith("data:")) { |
| 33 | + File file = new File(img); |
| 34 | + bufferedImage = ImageIO.read(file); |
| 35 | + imageInputStream = ImageIO.createImageInputStream(file); |
| 36 | + } else { |
| 37 | + String base64String = img.substring(img.indexOf("base64,") + 7); |
| 38 | + Base64.Decoder base64Decoder = Base64.getDecoder(); |
| 39 | + byte[] fileContent = base64Decoder.decode(base64String); |
| 40 | + ByteArrayInputStream bais = new ByteArrayInputStream(fileContent); |
| 41 | + bufferedImage = ImageIO.read(bais); |
| 42 | + |
| 43 | + ByteArrayInputStream baisDPI = new ByteArrayInputStream(fileContent); |
| 44 | + imageInputStream = ImageIO.createImageInputStream(baisDPI); |
| 45 | + } |
| 46 | + if (bufferedImage != null) { |
| 47 | + int width_px = bufferedImage.getWidth(); |
| 48 | + int height_px = bufferedImage.getHeight(); |
| 49 | + |
| 50 | + int image_dpi = getDPI(imageInputStream); |
| 51 | + |
| 52 | + double width_mm = Double.valueOf(width_px) / image_dpi * 25.4; |
| 53 | + double height_mm = Double.valueOf(height_px) / image_dpi * 25.4; |
| 54 | + |
| 55 | + //double width_effective_px = Double.valueOf(width_effective) / 25.4 * image_dpi; |
| 56 | + //double height_effective_px = Double.valueOf(height_effective) / 25.4 * image_dpi; |
| 57 | + double width_effective_mm = Double.valueOf(width_effective); |
| 58 | + double height_effective_mm = Double.valueOf(height_effective); |
| 59 | + |
| 60 | + |
| 61 | + double scale_x = 1.0; |
| 62 | + if (width_mm > width_effective_mm) { |
| 63 | + scale_x = width_effective_mm / width_mm; |
| 64 | + } |
| 65 | + |
| 66 | + double scale_y = 1.0; |
| 67 | + if (height_mm * scale_x > height_effective_mm) { |
| 68 | + scale_y = height_effective_mm / (height_mm * scale_x); |
| 69 | + } |
| 70 | + |
| 71 | + double scale = scale_x; |
| 72 | + if (scale_y != 1.0) { |
| 73 | + scale = scale_x * scale_y; |
| 74 | + } |
| 75 | + |
| 76 | + return String.valueOf(Math.round(scale * 100)); |
| 77 | + |
| 78 | + } |
| 79 | + } catch (Exception ex) { |
| 80 | + logger.log(Level.SEVERE, "Can''t read DPI from image: {0}", ex.toString()); |
| 81 | + } |
| 82 | + |
| 83 | + return "100"; |
| 84 | + } |
| 85 | + |
| 86 | + private static int getDPI(ImageInputStream imageInputStream) { |
| 87 | + int default_DPI = 96; |
| 88 | + if (imageInputStream != null) { |
| 89 | + try { |
| 90 | + Iterator<ImageReader> readers = ImageIO.getImageReaders(imageInputStream); |
| 91 | + if (readers.hasNext()) { |
| 92 | + ImageReader reader = readers.next(); |
| 93 | + reader.setInput(imageInputStream); |
| 94 | + |
| 95 | + IIOMetadata metadata = reader.getImageMetadata(0); |
| 96 | + IIOMetadataNode standardTree = (IIOMetadataNode) metadata.getAsTree(IIOMetadataFormatImpl.standardMetadataFormatName); |
| 97 | + IIOMetadataNode dimension = (IIOMetadataNode) standardTree.getElementsByTagName("Dimension").item(0); |
| 98 | + float pixelSizeMM = getPixelSizeMM(dimension, "HorizontalPixelSize"); |
| 99 | + if (pixelSizeMM == -1.0f) { // try get verrical pixel size |
| 100 | + pixelSizeMM = getPixelSizeMM(dimension, "VerticalPixelSize"); |
| 101 | + } |
| 102 | + if (pixelSizeMM == -1.0f) return default_DPI; |
| 103 | + float dpi = (float) (25.4f / pixelSizeMM); |
| 104 | + return Math.round(dpi); |
| 105 | + } |
| 106 | + } catch (Exception ex) { |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + logger.log(Level.SEVERE, "Could not read image DPI, use default value {0} DPI", default_DPI); |
| 111 | + return default_DPI; //default DPI |
| 112 | + } |
| 113 | + |
| 114 | + private static float getPixelSizeMM(final IIOMetadataNode dimension, final String elementName) { |
| 115 | + NodeList pixelSizes = dimension.getElementsByTagName(elementName); |
| 116 | + IIOMetadataNode pixelSize = pixelSizes.getLength() > 0 ? (IIOMetadataNode) pixelSizes.item(0) : null; |
| 117 | + return pixelSize != null ? Float.parseFloat(pixelSize.getAttribute("value")) : -1; |
| 118 | + } |
| 119 | + |
| 120 | + public static String convertWebPtoPNG(String img) { |
| 121 | + try { |
| 122 | + BufferedImage bufferedImage; |
| 123 | + if (!img.startsWith("data:")) { |
| 124 | + File file = new File(img); |
| 125 | + String filenameOut = img.substring(0, img.lastIndexOf(".")) + ".png"; |
| 126 | + File fileOut = new File(filenameOut); |
| 127 | + bufferedImage = ImageIO.read(file); |
| 128 | + ImageIO.write(bufferedImage, "png", fileOut); |
| 129 | + return filenameOut; |
| 130 | + } else { |
| 131 | + String base64String = img.substring(img.indexOf("base64,") + 7); |
| 132 | + Base64.Decoder base64Decoder = Base64.getDecoder(); |
| 133 | + byte[] fileContent = base64Decoder.decode(base64String); |
| 134 | + ByteArrayInputStream bais = new ByteArrayInputStream(fileContent); |
| 135 | + bufferedImage = ImageIO.read(bais); |
| 136 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 137 | + ImageIO.write(bufferedImage, "png", baos); |
| 138 | + byte[] imgPNGbytes = baos.toByteArray(); |
| 139 | + String imgPNGbase64 = Base64.getEncoder().encodeToString(imgPNGbytes); |
| 140 | + return "data:image/png;base64," + imgPNGbase64; |
| 141 | + } |
| 142 | + } catch (Exception ex) { |
| 143 | + logger.log(Level.SEVERE, "Can''t read the image: {0}", ex.toString()); |
| 144 | + } |
| 145 | + return img; |
| 146 | + } |
| 147 | +} |
0 commit comments