1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
| import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils;
import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.util.List;
public class ImageUtil {
public static BufferedImage[] splitImage(BufferedImage image, int rows, int cols) { int chunks = rows * cols; int chunkWidth = image.getWidth() / cols; int chunkHeight = image.getHeight() / rows; int count = 0; BufferedImage[] imgs = new BufferedImage[chunks]; for (int x = 0; x < rows; x++) { for (int y = 0; y < cols; y++) { imgs[count] = new BufferedImage(chunkWidth, chunkHeight, BufferedImage.TYPE_INT_RGB); Graphics2D gr = imgs[count].createGraphics(); imgs[count] = gr.getDeviceConfiguration().createCompatibleImage(chunkWidth, chunkHeight, Transparency.TRANSLUCENT); gr.dispose(); gr = imgs[count].createGraphics(); gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth * y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null); gr.dispose(); count++; } } return imgs; }
public static byte[] imageToBytes(BufferedImage bufferedImage) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "png", baos); baos.flush(); byte[] imageInByte = baos.toByteArray(); baos.close(); return imageInByte;
}
public static byte[] mergeImageBytes(ImageMerge imageMerge, List<byte[]> bytes) { bytes.removeIf(i -> i == null || i.length == 0); if (CollectionUtils.isEmpty(bytes)) { return new byte[0]; } BufferedImage read = null; ByteArrayInputStream in = new ByteArrayInputStream(bytes.get(0)); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { read = ImageIO.read(in); for (int i = 1; i < bytes.size(); i++) { ByteArrayInputStream in1 = new ByteArrayInputStream(bytes.get(i)); BufferedImage image1 = ImageIO.read(in1); read = switch (imageMerge) { case HORIZONTAL -> horizontal(read, image1); case VERTICAL -> vertical(read, image1); default -> nested(read, image1); }; } ImageIO.write(read, "png", out); } catch (IOException e) { LoggerUtil.error("read image byte error,exception:", e); } return out.toByteArray(); }
public static byte[] mergeImage(ImageMerge merge, List<String> images) { byte[] bytes = new byte[0]; try { BufferedImage image1 = ImageIO.read(new File(images.get(0))); for (int i = 1; i < images.size(); i++) { File file = new File(images.get(i)); if (!file.isFile()) { continue; } BufferedImage image2 = ImageIO.read(file); image1 = switch (merge) { case HORIZONTAL -> horizontal(image1, image2); case VERTICAL -> vertical(image1, image2); default -> nested(image1, image2); }; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(image1, "png", baos); baos.flush(); return baos.toByteArray(); } catch (IOException e) { LoggerUtil.error("read image error,exception:", e); } return bytes; }
private static BufferedImage horizontal(BufferedImage image1, BufferedImage image2) { BufferedImage image = new BufferedImage(image1.getWidth() + image2.getWidth(), Math.max(image1.getHeight(), image2.getHeight()), BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); g.drawImage(image1, 0, 0, null); g.drawImage(image2, image1.getWidth(), 0, null); g.dispose(); return image; }
private static BufferedImage vertical(BufferedImage image1, BufferedImage image2) { BufferedImage image = new BufferedImage(Math.max(image1.getWidth(), image2.getWidth()), image1.getHeight() + image2.getHeight(), BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(image1, 0, 0, null); g.drawImage(image2, 0, image1.getHeight(), null); g.dispose(); return image; }
private static BufferedImage nested(BufferedImage image1, BufferedImage image2) { BufferedImage image = new BufferedImage(Math.max(image1.getWidth(), image2.getWidth()), Math.max(image1.getHeight(), image2.getHeight()), BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.drawImage(image1, 0, 0, null); g.drawImage(image2, 0, 0, null); g.dispose(); return image; }
public static byte[] readImage(String filePath) { if (StringUtils.isBlank(filePath)) { return null; } try { BufferedImage read = ImageIO.read(new File(filePath)); ByteArrayOutputStream out = new ByteArrayOutputStream(); ImageIO.write(read, "png", out); return out.toByteArray(); } catch (IOException e) { LoggerUtil.error("读取图片失败,exception:", e); return null; } }
public enum ImageMerge { HORIZONTAL, VERTICAL, NESTED } }
|