j2me 放大缩小图片代码

昨天去面试遇到了这么一道题,回来就按插值算法写了下,代码如下:

 

public Image zoomImage(Image src, int desW, int desH) {
  Image returnImg = null;
  int srcW = src.getWidth();// 源图片的宽
  int srcH = src.getHeight();// 源图片的高
  int srcPix[] = new int[srcW * srcH];// 存储源图片像素点信息的数组
  int tabY[] = new int[desH];// 转成目标图片使用的存储插值横行信息的插值表
  int tabX[] = new int[desW];// 转成目标图片使用的存储插值竖行信息的插值表
  int distance;// 在做插值表的时候根据源图和目标图比较,看那个长度作为标准
  /*
   * ========================================基础变量==========================
   */

  src.getRGB(srcPix, 0, srcW, 0, 0, srcW, srcH);// 将源图片像素信息转存至srcPix数组
  distance = srcH > desH ? srcH : desH;// 哪个大哪个作为标准
  int sPoint = 0;
  int dPoint = 0;
  int sFoot = 0;
  int dFoot = 0;
  for (int i = 0; i <= distance; i++) {
   tabY[dPoint] = sPoint;
   sFoot += srcH;
   dFoot += desH;
   if (sFoot > distance) {
    sPoint++;
    sFoot -= distance;
   }
   if (dFoot > distance) {
    dPoint++;
    dFoot -= distance;
   }
  }
  sPoint = 0;
  dPoint = 0;
  sFoot = 0;
  dFoot = 0;
  distance = srcW > desW ? srcW : desW;
  for (int i = 0; i <= distance; i++) {
   tabX[dPoint] = sPoint;
   sFoot += srcW;
   dFoot += desW;
   if (sFoot > distance) {
    sPoint++;
    sFoot -= distance;
   }
   if (dFoot > distance) {
    dPoint++;
    dFoot -= distance;
   }
  }
  /*-------------------插值表录入完成-----------------*/
  int desPix[] = new int[desW * desH];
  int mubSH = 0;
  int mubDH = 0;
  int remDYP = -1;// 记录插值表Y到哪个点了。
  for (int i = 0; i < desH; i++) {
   if (remDYP == tabY[i]) {
    System.arraycopy(desPix, mubDH - desW, desPix, mubDH, desW);
   } else {
    for (int j = 0; j < desW; j++) {
     desPix[mubDH + j] = srcPix[mubSH + tabX[j]];
    }
    mubSH += srcW;
    remDYP = tabY[i];
   }
   mubDH += desW;
  }
  returnImg = Image.createRGBImage(desPix, desW, desH, false);
  return returnImg;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值