java图片处理(背景色透明、颜色替换)

您所在的位置:网站首页 透明色背景 java图片处理(背景色透明、颜色替换)

java图片处理(背景色透明、颜色替换)

2023-11-23 03:32| 来源: 网络整理| 查看: 265

 

作为一个完全的小白,折腾了一个星期多,才完成一张图片的处理,包括添加logo,二维码,以及图片的部分细节处理

期间也参考了部分资料:https://www.cnblogs.com/xiaoduilantian/p/9206170.html

https://blog.csdn.net/jzshmyt/article/details/8364408

https://blog.csdn.net/yellowd1/article/details/50879296

 

 

import javax.imageio.ImageIO;import

javax.swing.*;import java.awt.*;

import java.awt.image.BufferedImage;import

java.io.File;import java.io.IOException;

 

//处理透明的图片格式为png

public class Change {

  public static void convert(String path,String outFile)throws IOException {

       //将背景色变透明

      try{

           BufferedImage image = ImageIO.read(new File(path));

           ImageIcon imageIcon = new ImageIcon(image);

           int w = imageIcon.getIconWidth();

           int h = imageIcon.getIconHeight();

           BufferedImage bufferedImage = new BufferedImage(w,h,BufferedImage.TYPE_4BYTE_ABGR);

           Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();

           g2D.drawImage(imageIcon.getImage(),0,0,imageIcon.getImageObserver());

           int alpha = 0;

           for(int i = bufferedImage.getMinX();i

                   int rgb = bufferedImage.getRGB(i,j);

                   //以背景色左上角最小像素做参考系

                   int RGB = bufferedImage.getRGB(bufferedImage.getMinX(),bufferedImage.getMinY());

                   int r = (rgb & 0xff0000) >>16;

                   int g = (rgb & 0xff00) >> 8;

                   int b = (rgb & 0xff);

                  int R = (RGB & 0xff0000) >>16;

                   int G = (RGB & 0xff00) >> 8;

                   int B = (RGB & 0xff);

//a为色差范围值,渐变色边缘处理,数值需要具体测试,50左右的效果比较可以

                   int a = 45;

                  if(Math.abs(R-r)

      //指定某种单色为透明

       try{

          BufferedImage image = ImageIO.read(new File(imagePath));

           ImageIcon imageIcon = new ImageIcon(image);

           int w = imageIcon.getIconWidth();

           int h = imageIcon.getIconHeight();

           BufferedImage bufferedImage = new BufferedImage(w,h,BufferedImage.TYPE_4BYTE_ABGR);

           Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics();

           g2D.drawImage(imageIcon.getImage(),0,0,imageIcon.getImageObserver());

           int alpha = 0;

          for(int i = bufferedImage.getMinX();i

                   int rgb = bufferedImage.getRGB(i,j);

                   //以白色为例

                   int RGB = Color.WHITE.getRGB();

                  int r = (rgb & 0xff0000) >>16;

                   int g = (rgb & 0xff00) >> 8;

                  int b = (rgb & 0xff);

                  int R = (RGB & 0xff0000) >>16;

                   int G = (RGB & 0xff00) >> 8;

                   int B = (RGB & 0xff);

                   if(Math.abs(R-r)

           e.printStackTrace();

       }

  }

 

  public static void main(String[] args)throws IOException{

      //原图路径:E:\\png格式logo\\H1.png

     try{

           convert("原图路径","保存后路径");

       }catch (IOException e){

           e.printStackTrace();

   }

      try{

          convert2("原图路径","保存后路径");

   }catch (IOException e){

       e.printStackTrace();

  }

   try{

           convert3("E:\\原图路径","保存后路径");

   }catch (IOException e){

       e.printStackTrace();

   }

}

    public static void convert3(String imagePath,String outFile)throws IOException {

       //指定某种颜色替换成另一种

           BufferedImage image = null;

           image = ImageIO.read(new File(imagePath));

           int w = image.getWidth();

           int h = image.getHeight();

           int minx = image.getMinTileX();

           int miny = image.getMinTileY();

           for(int i = minx;i

                  int rgb = image.getRGB(i,j);

                   //以黑色为例

                   int RGB = Color.BLACK.getRGB();

                   int r = (rgb & 0xff0000) >>16;

                   int g = (rgb & 0xff00) >> 8;

                   int b = (rgb & 0xff);

                   int R = (RGB & 0xff0000) >>16;

                  int G = (RGB & 0xff00) >> 8;

                   int B = (RGB & 0xff);

                  if(Math.abs(R-r)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3