|
| 1 | +/* |
| 2 | + * Copyright © 2021 <a href="mailto:zhang.h.n@foxmail.com">Zhang.H.N</a>. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (thie "License"); |
| 5 | + * You may not use this file except in compliance with the license. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://wwww.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language govering permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.gcszhn.autocard.utils; |
| 17 | + |
| 18 | +import java.awt.image.BufferedImage; |
| 19 | +import java.io.ByteArrayInputStream; |
| 20 | +import java.io.ByteArrayOutputStream; |
| 21 | +import java.io.File; |
| 22 | +import java.io.FileOutputStream; |
| 23 | +import java.util.Base64; |
| 24 | + |
| 25 | +import javax.imageio.ImageIO; |
| 26 | + |
| 27 | +import java.awt.Graphics2D; |
| 28 | +import java.awt.Image; |
| 29 | + |
| 30 | +public class ImageUtils { |
| 31 | + public static BufferedImage resize(BufferedImage image, int width, int height) { |
| 32 | + BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); |
| 33 | + Image tmp = image.getScaledInstance(width, height, Image.SCALE_SMOOTH); |
| 34 | + Graphics2D g2d = newImage.createGraphics(); |
| 35 | + g2d.drawImage(tmp, 0, 0, null); |
| 36 | + g2d.dispose(); |
| 37 | + return newImage; |
| 38 | + } |
| 39 | + |
| 40 | + public static String toBase64(BufferedImage image, String formatName) { |
| 41 | + try { |
| 42 | + return Base64.getEncoder().encodeToString(toByteArray(image, formatName)); |
| 43 | + } catch (Exception e) { |
| 44 | + LogUtils.printMessage(null, e, LogUtils.Level.ERROR); |
| 45 | + } |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + public static byte[] toByteArray(BufferedImage image, String formatName) { |
| 50 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 51 | + ImageIO.write(image, formatName , outputStream); |
| 52 | + return outputStream.toByteArray(); |
| 53 | + } catch (Exception e) { |
| 54 | + LogUtils.printMessage(null, e, LogUtils.Level.ERROR); |
| 55 | + } |
| 56 | + return null; |
| 57 | + } |
| 58 | + |
| 59 | + public static BufferedImage toImage(String base64) { |
| 60 | + try { |
| 61 | + return toImage(Base64.getDecoder().decode(base64)); |
| 62 | + } catch (Exception e) { |
| 63 | + LogUtils.printMessage(null, e, LogUtils.Level.ERROR); |
| 64 | + } |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + public static BufferedImage toImage(byte[] byteArray) { |
| 69 | + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(byteArray)) { |
| 70 | + return ImageIO.read(inputStream); |
| 71 | + } catch (Exception e) { |
| 72 | + LogUtils.printMessage(null, e, LogUtils.Level.ERROR); |
| 73 | + } |
| 74 | + return null; |
| 75 | + } |
| 76 | + |
| 77 | + public static void write(BufferedImage image, String formatName, File file) { |
| 78 | + try { |
| 79 | + ImageIO.write(image, formatName, file); |
| 80 | + } catch (Exception e) { |
| 81 | + LogUtils.printMessage(null, e, LogUtils.Level.ERROR); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public static void write(byte[] byteArray, File file) { |
| 86 | + try (FileOutputStream outputStream = new FileOutputStream(file)) { |
| 87 | + outputStream.write(byteArray); |
| 88 | + } catch (Exception e) { |
| 89 | + LogUtils.printMessage(null, e, LogUtils.Level.ERROR); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static void write(String base64, File file) { |
| 94 | + try { |
| 95 | + write(Base64.getDecoder().decode(base64), file); |
| 96 | + } catch (Exception e) { |
| 97 | + LogUtils.printMessage(null, e, LogUtils.Level.ERROR); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments