| | |
| | | import java.awt.*; |
| | | import java.awt.image.BufferedImage; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.HttpURLConnection; |
| | |
| | | |
| | | public class ImageUtils { |
| | | |
| | | /** |
| | | * 几种常见的图片格式 |
| | | */ |
| | | public static String IMAGE_TYPE_GIF = "gif";// 图形交换格式 |
| | | public static String IMAGE_TYPE_JPG = "jpg";// 联合照片专家组 |
| | | public static String IMAGE_TYPE_JPEG = "jpeg";// 联合照片专家组 |
| | | public static String IMAGE_TYPE_BMP = "bmp";// 英文Bitmap(位图)的简写,它是Windows操作系统中的标准图像文件格式 |
| | | public static String IMAGE_TYPE_PNG = "png";// 可移植网络图形 |
| | | public static String IMAGE_TYPE_PSD = "psd";// Photoshop的专用格式Photoshop |
| | | |
| | | |
| | | /** |
| | | * 程序入口:用于测试 |
| | | * @param args |
| | | */ |
| | | public static void main(String[] args) { |
| | | // -图像类型转换: |
| | | ImageUtils.convert("D:/IMG_20180811_222034.png", "jpg", "D:/test.jpg");//测试OK |
| | | } |
| | | |
| | | /** |
| | | * 图像类型转换:GIF->JPG、GIF->PNG、PNG->JPG、PNG->GIF(X)、BMP->PNG |
| | | * @param srcImageFile 源图像地址 |
| | | * @param formatName 包含格式非正式名称的 String:如JPG、JPEG、GIF等 |
| | | * @param destImageFile 目标图像地址 |
| | | */ |
| | | public static void convert(String srcImageFile, String formatName, String destImageFile) { |
| | | try { |
| | | File f = new File(srcImageFile); |
| | | f.canRead(); |
| | | f.canWrite(); |
| | | BufferedImage src = ImageIO.read(f); |
| | | ImageIO.write(src, formatName, new File(destImageFile)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | public static String getBase64ByImgUrl(String url){ |