博主首页

图片转灰度、留白

    @SneakyThrows
    public static void z转灰度(String filePath,String filePath2) {
        //开始识别时间
        long startTime = System.currentTimeMillis();
        File file = new File(filePath);
        File file2 = new File(filePath2);
        BufferedImage image = ImageIO.read(file);
        int width = image.getWidth();
        int height = image.getHeight();
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int rgb = image.getRGB(i, j);
                Color color = new Color(rgb);
                int gray = (int) (color.getRed() * 0.299 + color.getGreen() * 0.587 + color.getBlue() * 0.114);
                Color color_end = new Color(gray, gray, gray);
                image.setRGB(i, j, color_end.getRGB());
            }
        }
        ImageIO.write(image, "jpg", file2);
        SLog.info("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
    }

    @SneakyThrows
    public static void l留白(String filePath,String filePath2) {
        //开始识别时间
        long startTime = System.currentTimeMillis();
        File file = new File(filePath);
        File file2 = new File(filePath2);
        BufferedImage image = ImageIO.read(file);
        int width = image.getWidth();
        int height = image.getHeight();
        for (int i = 0; i < width; i++) {
            for (int j = 0; j < height; j++) {
                int rgb = image.getRGB(i, j);
                Color color = new Color(rgb);
                int gray = (int) (color.getRed() * 0.299 + color.getGreen() * 0.587 + color.getBlue() * 0.114);
                Color color_end = new Color(gray, gray, gray);
                if(color_end.getGreen()>100&&color_end.getGreen()<150){
                    image.setRGB(i, j, color_end.getRGB());
                }else{
                    image.setRGB(i, j, Color.black.getRGB());
                }

            }
        }
        ImageIO.write(image, "jpg", file2);
        SLog.info("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
    }

 

posted @ 2024-07-15 11:41  笑~笑  阅读(5)  评论(0编辑  收藏  举报