xuekang
2024-05-11 bac0878349a1db23e7b420ea164e22fb9db73a99
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
package com.nuvole.util;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
 
//// @formatter:off
//import javax.imageio.*;import javax.imageio.metadata.IIOMetadata;import javax.imageio.stream.ImageOutputStream;import java.awt.*;import java.awt.image.BufferedImage;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream; /**
// *                .-~~~~~~~~~-._       _.-~~~~~~~~~-.
// *            __.'  @Author     ~.   .~    代码无Bug   `.__
// *          .'//     liu.q        \./       (秘籍)      \\`.
// *        .'// [916000612@qq.com]  |   欲练神功   引刀自宫  \\`.
// *      .'// .-~"""""""~~~~-._     |    _,-~~~~"""""""~-.  \\`.
// *    .'//.-"  2019-04-24     `-.  |  .-'     12:53       "-.\\`.
// *  .'//______.============-..   \ | /   ..-============.______\\`.
// *.'______________________________\|/______________________________`.
// *
// * @Description :
// */
//// @formatter:on
//
public class ImageUtil {
    public static void main(String[] args) throws Exception {
        byte[] imageBytes = getImageBytes("C:\\Users\\cy\\Pictures\\营业执照\\111.jpg");
        System.out.println(imageBytes.length);
    }
    public static byte[] getImageBytes(String imagePath) throws Exception {
        // 读取图片文件
        BufferedImage bufferedImage = ImageIO.read(new File(imagePath));
 
        // 将图片写入字节流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bufferedImage, "jpg", baos);
 
        // 返回字节数组
        return baos.toByteArray();
    }
//
//    /*
//     * @Author : liu.q [916000612@qq.com]
//     * @Date : 2019-04-24 12:55
//     * @param OldInputStream : 原图
//     * @param w : 宽
//     * @param h : 高
//     * @param quality : 质量
//     * @return : InputStream 压缩后的图
//     * @Description :
//     */
//    public static InputStream ImgCompress(InputStream OldInputStream,String suffix,int w, int h, float quality) {
//        try {
//            /** 处理宽高 */
//            BufferedImage bufferedImage =  ImageIO.read(OldInputStream);
//            int new_w = bufferedImage.getWidth(null);
//            int new_h = bufferedImage.getHeight(null);
//            if(new_w<=w || new_h<=h){//原图宽高小于要压缩的宽高就不压缩了。
//                return OldInputStream;
//            }
//            double bili;
//            if(w<=0 || h<=0){
//                if(w>0){
//                    bili=(double)w/new_w;
//                    new_h = (int) (new_h*bili);
//                    new_w=w;
//                }else{
//                    if(h>0){
//                        bili=(double)h/new_h;
//                        new_w = (int) (new_w*bili);
//                        new_h=h;
//                    }
//                }
//            }else{
//                new_w=w;
//                new_h=h;
//            }
//
//            BufferedImage image_to_save = new BufferedImage(new_w, new_h,bufferedImage.getType());
//            image_to_save.getGraphics().drawImage(bufferedImage.getScaledInstance(new_w, new_h, Image.SCALE_SMOOTH), 0,0, null);
//            ImageWriter imageWriter =  ImageIO.getImageWritersBySuffix(suffix).next();
//            IIOMetadata imageMetaData = imageWriter.getDefaultImageMetadata(new ImageTypeSpecifier(image_to_save), null);
//            //质量压缩
//            if (quality >= 0 && quality <= 1f) {
//                ImageWriteParam jpegParams = imageWriter.getDefaultWriteParam();
//                jpegParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
//                jpegParams.setCompressionQuality(quality);
//            }
//            //写入图片
//            imageWriter.write(imageMetaData,new IIOImage(image_to_save, null, null), null);
//            imageWriter.dispose();
//
//            return imageWriter;
//        } catch (IOException ex) {
//            ex.printStackTrace();
//        }
//    }
}