zoukankan      html  css  js  c++  java
  • Java读取图片并修改像素,创建图片

    public void replaceImageColor(String file, Color srcColor, Color targetColor) throws IOException{
            URL http;
            if(file.trim().startsWith("https")){
                http = new URL(file);
                HttpsURLConnection conn = (HttpsURLConnection) http.openConnection();
                conn.setRequestMethod("GET"); 
            }else if(file.trim().startsWith("http")){
                http = new URL(file);
                HttpURLConnection conn = (HttpURLConnection) http.openConnection();
                conn.setRequestMethod("GET"); 
            }else{
                http = new File(file).toURI().toURL();
            }
            BufferedImage bi = ImageIO.read(http.openStream());
            
            for (int i = 0; i < bi.getWidth(); i++) {
                for (int j = 0; j < bi.getHeight(); j++) {
                      System.out.println(bi.getRGB(i, j));
                      if(srcColor.getRGB()==bi.getRGB(i, j)){
                          System.out.println(i+","+j+"  from:"+srcColor.getRGB()+"to"+targetColor.getRGB());
                          bi.setRGB(i, j, targetColor.getRGB());
                      }
                }
            }
            Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("png");
            ImageWriter writer = it.next();
            File f = new File("c://test02.png");
            ImageOutputStream ios = ImageIO.createImageOutputStream(f);
            writer.setOutput(ios);
            writer.write(bi);
            bi.flush();
            ios.flush();
            ios.close();
        }
        
        public void createImage(int width, int height) throws IOException{
            BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D graphic = bi.createGraphics();
            graphic.setColor(new Color(0.2f,0.3f,0.4f,0.4f));
            graphic.fillRect(0, 0, width, height);
            
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                      //result[i][j] = bi.getRGB(i, j) & 0xFFFFFF;
                      System.out.println(bi.getRGB(i, j));
                     // bi.setRGB(i, j, 0xFFFFFF);
                }
           }
            
            Iterator<ImageWriter> it = ImageIO.getImageWritersByFormatName("png");
            ImageWriter writer = it.next();
            File f = new File("c://test02.png");
            ImageOutputStream ios = ImageIO.createImageOutputStream(f);
            writer.setOutput(ios);
            
            writer.write(bi);
        }
  • 相关阅读:
    Android中的httpclient框架发送get请求
    成员函数的重载&amp;&amp;隐藏&amp;&amp;覆盖
    子墨庖丁Android的ActionBar源代码分析 (一)实例化
    Hadoop2.x介绍与源代码编译
    NFS 服务器的配置
    tftp 服务器的配置
    LINUX内核及应用程序移植工作
    u-boot 移植工作目录
    Linux 下工作用户及环境
    zless
  • 原文地址:https://www.cnblogs.com/yoyogis/p/4170048.html
Copyright © 2011-2022 走看看