Esempio n. 1
0
  // ///////////////////////////////////////////////////////////////
  // 最临近插值算法----不考虑坐标的小数部分
  // 参数:img:要缩放的Image对象
  // dstW:目标图像宽
  // dstH:目标图像高
  // comp:组件参数,比如Applet
  // ///////////////////////////////////////////////////////////////
  public static Image simpleScale(Image img, int dstW, int dstH) {
    OperateImage OI = new OperateImage();
    int[] scaled, src;
    double widthFactor, heightFactor;
    int srcX = 0, srcY = 0, srcW, srcH;
    src = OI.takeImg(img, img.getWidth(null), img.getHeight(null));
    scaled = new int[dstW * dstH]; // 存放缩放后的图片
    srcW = img.getWidth(null);
    srcH = img.getHeight(null);

    widthFactor = srcW / (dstW + 0.0);
    // System.out.println("widthFactor:"+widthFactor);
    heightFactor = srcH / (dstH + 0.0);
    // System.out.println("heightFactor:"+heightFactor);
    for (int a = 0; a < dstH; a++)
      for (int b = 0; b < dstW; b++) {
        if ((b * widthFactor) % 1 >= 0.5) srcX = (int) (b * widthFactor) + 1;
        else srcX = (int) (b * widthFactor);
        if ((a * heightFactor) % 1 >= 0.5) srcY = (int) (a * heightFactor) + 1;
        else srcY = (int) (a * heightFactor);
        if (srcX > srcW - 1) srcX = srcW - 1;
        if (srcY > srcH - 1) srcY = srcH - 1;
        scaled[a * dstW + b] = src[srcY * srcW + srcX];
      }
    // System.out.println("最临近插值算法完成!");
    return OI.madeImg(scaled, dstW, dstH);
  }
Esempio n. 2
0
  // ///////////////////////////////////////////////////////////////
  // 双线性内插值算法
  // 参数:img:要缩放的Image对象
  // dstW:目标图像宽
  // dstH:目标图像高
  // comp:组件参数,比如Applet
  //
  // 公式:f(i+u,j+v) = (1-u)(1-v)f(i,j) + (1-u)vf(i,j+1) + u(1-v)f(i+1,j) +
  // uvf(i+1,j+1)
  //
  // ///////////////////////////////////////////////////////////////
  public static Image doubleLinearScale(Image img, int dstW, int dstH) {
    OperateImage OI = new OperateImage();
    Image imgTemp;
    int[] scaled, src;
    int srcW, srcH;
    int R, G, B;
    double widthFactor, heightFactor, tempX, tempY;
    // double srcX_float = 0.0, srcY_float = 0.0;// 坐标的小数部分
    // int srcX_int = 0, srcY_int = 0;// 坐标的整数部分
    src = OI.takeImg(img, img.getWidth(null), img.getHeight(null));
    ColorModel cm = ColorModel.getRGBdefault();
    for (int j = 0; j < src.length; j++) {
      R = cm.getRed(src[j]);
      G = cm.getGreen(src[j]);
      B = cm.getBlue(src[j]);
      if (R >= 200 && G >= 200 && B >= 200) src[j] = 0xffffffff;
      else src[j] = 0xff000000;
    }
    scaled = new int[dstW * dstH]; // 存放缩放后的图片
    srcW = img.getWidth(null);
    srcH = img.getHeight(null);

    widthFactor = srcW / (dstW + 0.0);
    // System.out.println("widthFactor:"+widthFactor);
    heightFactor = srcH / (dstH + 0.0);
    // System.out.println("heightFactor:"+heightFactor);
    for (int a = 0; a < dstH; a++)
      for (int b = 0; b < dstW; b++) {
        tempX = b * widthFactor;
        tempY = a * heightFactor;
        scaled[a * dstW + b] = getDestPixle(src, srcW, srcH, tempX, tempY);
      }
    // System.out.println("双线性内插值算法完成!");
    imgTemp = OI.madeImg(scaled, dstW, dstH);
    ImageFilter filter = new BWFilter();
    return Toolkit.getDefaultToolkit()
        .createImage(new FilteredImageSource(imgTemp.getSource(), filter));
    // return imgTemp;
  }
Esempio n. 3
0
  // ///////////////////////////////////////////////////////////////
  // 最临近插值算法--考虑坐标的小数部分
  // 参数:img:要缩放的Image对象
  // dstW:目标图像宽
  // dstH:目标图像高
  // comp:组件参数,比如Applet
  // ///////////////////////////////////////////////////////////////
  public static Image simpleScaleSpcial(Image img, int dstW, int dstH) {
    OperateImage OI = new OperateImage();
    int[] scaled, src;
    double widthFactor, heightFactor, tempX, tempY, distance, tempDis;
    double srcX_float = 0.0, srcY_float = 0.0; // 坐标的小数部分
    int srcX_int = 0, srcY_int = 0; // 坐标的整数部分
    int srcX = 0, srcY = 0, srcW, srcH;
    src = OI.takeImg(img, img.getWidth(null), img.getHeight(null));
    scaled = new int[dstW * dstH]; // 存放缩放后的图片
    srcW = img.getWidth(null);
    srcH = img.getHeight(null);

    widthFactor = srcW / (dstW + 0.0);
    // System.out.println("widthFactor:"+widthFactor);
    heightFactor = srcH / (dstH + 0.0);
    // System.out.println("heightFactor:"+heightFactor);
    for (int a = 0; a < dstH; a++)
      for (int b = 0; b < dstW; b++) {
        distance = Double.MAX_VALUE;
        tempX = b * widthFactor;
        tempY = a * heightFactor;
        srcX_int = (int) tempX;
        srcX_float = tempX % 1;
        srcY_int = (int) tempY;
        srcY_float = tempY % 1;
        tempDis = (srcX_float * srcX_float) + (srcY_float * srcY_float);
        if (tempDis < distance)
        // 左上角 (0,0)
        {
          srcX = srcX_int;
          srcY = srcY_int;
          distance = tempDis;
        }
        tempDis = ((srcX_float - 1) * (srcX_float - 1)) + (srcY_float * srcY_float);
        if (tempDis < distance)
        // 右上角 (1,0)
        {
          srcX = srcX_int + 1;
          srcY = srcY_int;
          distance = tempDis;
        }
        tempDis = (srcX_float * srcX_float) + ((srcY_float - 1) * (srcY_float - 1));
        if (tempDis < distance)
        // 左下角 (0,1)
        {
          srcX = srcX_int;
          srcY = srcY_int + 1;
          distance = tempDis;
        }
        tempDis = ((srcX_float - 1) * (srcX_float - 1)) + ((srcY_float - 1) * (srcY_float - 1));
        if (tempDis < distance)
        // 右下角 (1,1)
        {
          srcX = srcX_int + 1;
          srcY = srcY_int + 1;
        }
        if (srcX > srcW - 1) srcX = srcW - 1;
        if (srcY > srcH - 1) srcY = srcH - 1;
        scaled[a * dstW + b] = src[srcY * srcW + srcX];
      }
    // System.out.println("最临近插值算法(特殊)完成!");
    return OI.madeImg(scaled, dstW, dstH);
  }