把图片变换颜色!

来源:互联网 发布:散热模拟软件 编辑:程序博客网 时间:2024/04/30 09:15

class ImageCanvas
      extends Canvas {
    byte[] b;
    int imagelength;
    Image i;
    ImageCanvas() {
      imageRGBConvertInit();
      try {
        b = readImageToBytesEasy("/1.png", is);//Zane 40
//Zane  b = readImageToBytes("/1.png", is);//Zane 60
//Zane  b = readImageToBytes("/1.png", is,2355);//Zane 通用的
      }
      catch (IOException ioe) {}

      b = imageRGBConvert(b, 0, 0, 0, 0, 255, 0);
      if(b==null)
        System.out.println("Image file do not contain this COLOR!");
      try {
        i = Image.createImage(b, 0, b.length);
      }
      catch (Exception e) {
        System.out.println(e + "CREATEIMAGE");
      }
    }

    public void paint(Graphics g) {
      g.setColor(0, 0, 0);
      g.fillRect(0, 0, getWidth(), getHeight());
      g.drawImage(i, getWidth() / 2, getHeight() / 2, g.VCENTER | g.HCENTER);
    }

    public byte[] imageRGBConvert(byte[] image, int R, int G, int B, int cR,
                                  int cG, int cB) {
      int i;
      int crc;
      int length = 0;
      int temp;
      String[] sIDAT = {
          "49", "44", "41", "54"};
      String[] sPLTE = {
          "50", "4c", "54", "45"};
      int cnt = 0;
      if (image[1] != 0x50 ||
          image[2] != 0x4e ||
          image[3] != 0x47 ||
          image[4] != 0x0d ||
          image[5] != 0x0a ||
          image[6] != 0x1a ||
          image[7] != 0x0a) {
        System.out.println("INCORRECT");
        return null;
      }
      while (true) {
        for (i = 0; i < image.length; i++) {
          if (Integer.toHexString(image[i]).equals(sPLTE[cnt])) {
            cnt++;
            if (cnt >= 4) {
              for (int j = 4; j > 0; j--) {
                if (image[i - j - 3] != 0x00) {
                  switch (j) {
                    case 4:
                      length = image[i - j - 3] * 0xffffff;
                      break;
                    case 3:
                      length = image[i - j - 3] * 0xffff;
                      break;
                    case 2:
                      length = image[i - j - 3] * 0xff;
                      break;
                    case 1:
                      length = image[i - j - 3] * 1;
                      break;
                    default:
                      length = -1;
                      System.out.println("length ERROR!");
                      break;
                  }
                }
              }
              if(!changeColor(image, length, i + 1, R, G, B, cR, cG, cB))
                return null;//Zane 图片里没有这个颜色。
              cnt = 0;
              break;
            }
          }
          else {
            if (Integer.toHexString(image[i]).equals(sIDAT[cnt])) {
              cnt++;
              if (cnt >= 4) {
                System.out.println(
                    "This file do NOT contain PLTE!!!You should recreate your PNG file.");
                break;
              }
            }
            else {
              if (cnt != 0) {
                cnt = 0;
              }
              continue;
            }
          }
        }
        break;
      }

      crc = updateCRC(image, i - 3, length + 4);
      image[i + length + 4] = (byte) (crc >>> 0);
      image[i + length + 3] = (byte) (crc >>> 8);
      image[i + length + 2] = (byte) (crc >>> 16);
      image[i + length + 1] = (byte) (crc >>> 24);
      System.out.println("CRC==" + Integer.toHexString(crc));

      return image;
    }

    public byte[] readImageToBytes(String url, InputStream is, int length) throws
        IOException { //Zane 通用的,效率高
      System.out.println("readImageToBytes LENGTH");
      byte[] ref;
      is = getClass().getResourceAsStream(url);
      //System.out.println(is.available());
      ref = new byte[length];
      is.read(ref);
      is.close();
      System.gc();
      return ref;
    }

    public byte[] readImageToBytesEasy(String url, InputStream is) throws
        IOException { //Zane for 40 效率低

      byte[] ref;
      is = getClass().getResourceAsStream(url);
      int length = 0;
      while (is.read() != -1) {
        length++;
      }
      System.out.println("length==" + length);

      is = getClass().getResourceAsStream(url);
      ref = new byte[length];
      is.read(ref);
      is.close();
      System.gc();
      return ref;

    }

    public byte[] readImageToBytes(String url, InputStream is) throws
        IOException { //Zane for 60 , 在60上支持is.available()
      byte[] ref;
      is = getClass().getResourceAsStream(url);
      ref = new byte[is.available()];
      is.read(ref);
      is.close();
      System.gc();
      return ref;

    }

    public boolean changeColor(byte[] image, int length, int i, int r, int g,
                               int b, int r1,
                               int g1, int b1) {
      int temp = i;
      while (true) {
        if (i - temp > length) {
          return false;
        }
          if (image[i] == getValue(r) && image[i + 1] == getValue(g) &&
            image[i + 2] == getValue(b)) {
          image[i] = getValue(r1);
          image[i + 1] = getValue(g1);
          image[i + 2] = getValue(b1);
          return true;
        }
        else {
          i += 3;
        }
      }
    }

    public byte getValue(int i) {
      return i <= -1 ? (byte) (i + 256) : (byte) (i);
    }
  }

//Zane 重新生成CRC校验

  static int[] crcTable = new int[256];
  public void imageRGBConvertInit() {
    // Initialize CRC table
    for (int n = 0; n < 256; n++) {
      int c = n;
      for (int k = 0; k < 8; k++) {
        if ( (c & 1) == 1) {
          c = 0xedb88320 ^ (c >>> 1);
        }
        else {
          c >>>= 1;
        }

        crcTable[n] = c;
      }
    }
  }

  public int updateCRC(byte[] data, int off, int len) {
    int c = 0xffffffff;

    for (int n = 0; n < len; n++) {
      c = crcTable[ (c ^ data[off + n]) & 0xff] ^ (c >>> 8);
    }

    return c ^ 0xffffffff;
  }

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 海岛奇兵杯越来越多打不玩家怎么办 海岛奇兵控杯技巧 杯数太高怎么办 海岛奇兵发现求救信号怎么办 海岛奇兵被打了怎么办 小鱼翅卡喉咙了怎么办 鱼翅卡在喉咙里怎么办 斗鱼身份证被使用怎么办 做的鱼丸太腥了怎么办 做鱼丸太稀了怎么办 斗鱼手机号换了怎么办 斗鱼直播掉帧怎么办 手机一直卡顿点不动怎么办呢 斗鱼直播分值底怎么办 斗鱼6000鱼丸怎么办卡 斗鱼直播没人看怎么办 淘宝直播间没人气怎么办 挂水了还是有热度怎么办 陌陌工会不结算工资怎么办 滴滴给了差评怎么办 饿了么星级低怎么办 滴滴乘客给低星怎么办 蘑菇街自动收货前还没到怎么办 小主播人气太少别人看不到怎么办 税收分类编码不可用怎么办 斗鱼鱼翅充错了怎么办 苹果指纹摔坏了怎么办 小米5指纹坏了怎么办 苹果5s指纹失灵怎么办 学生赌博输了3万怎么办 电脑录屏没有声音怎么办 别人说你没有他美怎么办 没有你我怎么办歌词是什么意思 要是没有他我怎么办啊歌词 用喀秋莎保存的视频黑屏怎么办 电脑杀毒之后开不了机怎么办 夫妻离婚分房分车怎么办 请的护身符丢了怎么办 老车轻微烧机油怎么办 电脑下软件变卡怎么办 机械表日历偏了怎么办 子宫内膜异位痛经怎么办