Java究竟能够处理图片到什么程度

1、将图片圆角化处理

这是咱们要处理的图片

public String roundCornerImage(){
        String fileInPath = "D:\\Frozen\\testFile\\image\\1.jpg";//图像源
        String fileOurPath = "D:\\Frozen\\testFile\\image\\2.png";//图像输出
        String type = "png";
        try {
            BufferedImage image = ImageIO.read(new File(fileInPath));
            int w = image.getWidth();
            int h = image.getHeight();
            BufferedImage output = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            Graphics2D g2 = output.createGraphics();
            output = g2.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
            g2.dispose();
            g2 = output.createGraphics();
            //这里绘画原型图
            Ellipse2D.Double shape = new Ellipse2D.Double(0, 0,w, h);
            g2.setClip(shape);
            g2.drawImage(image, 0, 0, w, h, null);
            g2.dispose();
            ImageIO.write(output, type, new File(fileOurPath));
            return fileOurPath;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

效果

2、图片添加水印

 public String textToImage(){
        String fileInPath = "D:\\Frozen\\testFile\\image\\1.jpg";//图像源
        String fileOutPath = "D:\\Frozen\\testFile\\image\\3.jpg";//图像输出
        String text = "明明你也很爱我,没理由爱不到结果!";//水印的内容
        String fontName = "华文行楷";//字体名称
        int fontStyle = Font.ITALIC;//字体样式:粗体Font.BOLD|斜体Font.ITALIC
        int fontSize = 36;//字体大小
        int x = 212;//x轴
        int y = 100;//y轴
        float tmd = 0.7f;//透明度:0.0 -- 1.0, 0.0为完全透明,1.0为完全不透明
        try {
            File file = new File(fileInPath);
            Image image = ImageIO.read(file);
            int width = image.getWidth(null);
            int height = image.getHeight(null);
            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bufferedImage.createGraphics();
            g.drawImage(image,0,0, width, height, null);
            g.setFont(new Font(fontName, fontStyle, fontSize));
            g.setColor(Color.PINK);//awt颜色
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, tmd));
            //计算文字像素长度
            int textLength = text.length();
            int length = textLength;
            for (int i = 0; i < textLength; i++) {
            int wordLength = String.valueOf(text.charAt(i)).getBytes().length;
               if(wordLength > 1){
                length+=(wordLength-1);
                }
               }
            int width_wi = fontSize*(length%2==0 ? length/2:length/2+1);
            int height_wi = fontSize;
            int widthDiff = width-width_wi;
            int heightDiff = height-height_wi;
            if(x < 0){
                x = widthDiff/2;
            }else if(x > widthDiff){
                x=widthDiff;
            }
            if(y<0){
                y = heightDiff/2;
            }else if(y > heightDiff){
                y = heightDiff;
            }
            g.drawString(text, x, y + height_wi);//水印文件
            g.dispose();
            File fileo = new File(fileOutPath);
            ImageIO.write(bufferedImage, "JPEG", fileo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

效果

3、直接修改图片尺寸

  public String fixImageSize(){
        String fileIn = "D:\\Frozen\\testFile\\image\\1.jpg";
        String fileOut = "D:\\Frozen\\testFile\\image\\4.jpg";
        int width = 200;
        int height = 200;
        try {
            //把生成的圆形图片变换成宽高142x142的图片
            Thumbnails.of(fileIn).size(width, height).toFile(
                    fileOut);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

效果

 

4、图片合并

    @GetMapping("/redant12")
    public String hbImage(){
        String file1 = "D:\\Frozen\\testFile\\image\\2.jpg";//图片2:被贴
        String file2 = "D:\\Frozen\\testFile\\image\\4.jpg";//图片4:贴图
        String fileOut = "D:\\Frozen\\testFile\\image\\5.jpg";//图片5:输出
        try {
           Thumbnails.of(file1).size(1280, 1024)
                   .watermark(Positions.TOP_LEFT,
                    ImageIO.read(new File(file2)), 1f)
                    .outputQuality(0.8f).toFile(fileOut);
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

效果

5、图片旋转

   @GetMapping("/redant13")
    public String xzImage(){
        String fileIn = "D:\\Frozen\\testFile\\image\\1.jpg";
        String fileOu = "D:\\Frozen\\testFile\\image\\6.jpg";
        int width = 400;
        int height = 400;
        float xuanz = 85.5f;//正数:顺时针 负数:逆时针
        try {
            Thumbnails.of(fileIn).size(width, height).rotate(xuanz).toFile(fileOu);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

效果

参考地址:http://rensanning.iteye.com/blog/1545708

6、背景替换

    public String replaceBackImage(){
        String sourcePath = "D:\\Frozen\\testFile\\image\\back\\";
        File dir = new File(sourcePath);//要处理的图片目录
        File[] files = dir.listFiles();//列出目录中的图片,得到数组
        for(int x=0;x<files.length;x++){
            //定义一个RGB的数组,因为图片的RGB模式是由三个 0-255来表示的,比如白色就是(255,255,255)
            int[] rgb = new int[3];
            BufferedImage bi = null;
            try {
                bi = ImageIO.read(files[x]);
            } catch (Exception e) {
                e.printStackTrace();
            }
            //得到图片基本属性
            int width = bi.getWidth();
            int height = bi.getHeight();
            int minx = bi.getMinX();
            int miny = bi.getMinY();
            //按照长和宽遍历每一个像素,将像素上的颜色替换成目标颜色
            for (int i = minx; i < width; i++) {
                for (int j = miny; j < height; j++) {
                    //获取当前像素的颜色
                    int pixel = bi.getRGB(i, j);
                    //分别得到 R G B
                    rgb[0] = (pixel & 0xff0000) >> 16;
                    rgb[1] = (pixel & 0xff00) >> 8;
                    rgb[2] = (pixel & 0xff);
                    /**
                     * 进行换色操作,我这里是要把蓝底换成粉红底,
                     * 那么就判断图片中rgb值是否在蓝色范围的像素
                     */
                    //进行颜色替换
                    //蓝色的取值范围我也不清楚,这里是用取色工具比较的值
                   if(rgb[0] < 10 && rgb[1] < 150&& rgb[1] > 50 && rgb[2] > 150){
                       bi.setRGB(i, j, 0xFFC0CB);
                   }
                }
            }
            /**
             * 将缓冲对象保存到新文件中
             */
            try {
                FileOutputStream ops = new FileOutputStream(new File(sourcePath+x+".jpg"));
                ImageIO.write(bi,"jpg", ops);
                ops.flush();
                ops.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return null;
    }

效果

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值