KKSU
2024-05-30 84599f1fdac0d9723c7f02a2e66b0e909ad911d7
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
//package cc.mrbird.febs.mall.test;
//
//import com.sun.image.codec.jpeg.JPEGCodec;
//import com.sun.image.codec.jpeg.JPEGEncodeParam;
//import com.sun.image.codec.jpeg.JPEGImageEncoder;
//
//import javax.imageio.ImageIO;
//import java.awt.*;
//import java.awt.image.BufferedImage;
//import java.io.File;
//import java.io.FileOutputStream;
//import java.io.IOException;
//
//public class PixelateImage {
//
//    public static void main(String[] args) {
//        handleDpi(new File("D:\\image\\inputDpi.png"),50,50);
//        getPixel("D:\\image\\input.png");
//        try {
//            resizeImage("D:\\image\\input.png","D:\\image\\output.png",360);//将图片压缩至100宽
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//
//    /**
//     * 功能:获取图片像素
//     * * @param filePath 图片路径
//     */
//    public static void getPixel(String filePath){
//        File file = new File(filePath);
//        BufferedImage bi = null;
//        try {
//            bi = ImageIO.read(file);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        int width = bi.getWidth(); // 像素
//        int height = bi.getHeight(); // 像素
//        System.out.println("width=" + width + ",height=" + height + ".");
//    }
//
//
//    /**
//     * @param inputPath  源图片路径
//     * @param outputPath  修改大小后图片路径
//     * @param scaleSize 图片的修改比例,目标宽度
//     */
//    public static void resizeImage(String inputPath, String outputPath,int scaleSize) throws IOException {
//
//        File srcFile = new File(inputPath);
//        Image srcImg = ImageIO.read(srcFile);
//        BufferedImage bi = null;
//        try {
//            bi = ImageIO.read(srcFile);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        float width = bi.getWidth(); // 像素
//        float height = bi.getHeight(); // 像素
//        float scale = width/scaleSize;
//        BufferedImage buffImg = null;
//        buffImg = new BufferedImage(scaleSize, (int)(height/scale), BufferedImage.TYPE_INT_RGB);
//        //使用TYPE_INT_RGB修改的图片会变色
//        buffImg.getGraphics().drawImage(
//                srcImg.getScaledInstance(scaleSize, (int)(height/scale), Image.SCALE_SMOOTH), 0,
//                0, null);
//
//        ImageIO.write(buffImg, "JPEG", new File(outputPath));
//    }
//
//    /**
//     * 改变图片DPI
//     *
//     * @param file
//     * @param xDensity
//     * @param yDensity
//     */
//    public static void handleDpi(File file, int xDensity, int yDensity) {
//        try {
//            BufferedImage image = ImageIO.read(file);
//            JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(new FileOutputStream(file));
//            JPEGEncodeParam jpegEncodeParam = jpegEncoder.getDefaultJPEGEncodeParam(image);
//            jpegEncodeParam.setDensityUnit(JPEGEncodeParam.DENSITY_UNIT_DOTS_INCH);
//            jpegEncoder.setJPEGEncodeParam(jpegEncodeParam);
//            jpegEncodeParam.setQuality(0.1f, false);
//            jpegEncodeParam.setXDensity(xDensity);
//            jpegEncodeParam.setYDensity(yDensity);
//            jpegEncoder.encode(image, jpegEncodeParam);
//            image.flush();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }
//}