如何在J2ME里放大和缩小图片

本例的代码取自我写的上阳图像处理演示程序(ImageAlbum),

大家可以下载(包括所有源代码)看看实际效果

http://blog.csdn.net/the3gwireless/archive/2006/04/27/679608.aspx

 /***************************************************************************
  * 放大图片的方法
  */
 public static Image zoomInImage(Image img) {

  int[] rgbOutput = null;
  int width = 0, height = 0;
  int[] rgbInput = null;
  try {

   width = img.getWidth();
   height = img.getHeight();
   rgbInput = new int[width * height];
   rgbOutput = new int[(width << 1) * (height << 1)];
   img.getRGB(rgbInput, 0, width, 0, 0, width, height);
   int i, j, k;
   k = 0;
   for (i = 0; i < (height << 1); i += 2) {
    for (j = 0; j < (width << 1); j += 2) {
     if (j < 3) {
      // System.out.println(i * (width << 1) + j);
      // System.out.println((i + 1) * (width << 1) + j);
      // System.out.println(i * (width << 1) + j + 1);
      // System.out.println((i + 1) * (width << 1) + j + 1);
     }
     rgbOutput[i * (width << 1) + j] = rgbInput[k];
     rgbOutput[(i + 1) * (width << 1) + j] = rgbInput[k];
     rgbOutput[i * (width << 1) + j + 1] = rgbInput[k];
     rgbOutput[(i + 1) * (width << 1) + j + 1] = rgbInput[k];
     k++;
    }
   }
   return Image.createRGBImage(rgbOutput, width << 1, height << 1,
     true);
  } catch (OutOfMemoryError e) {
   // e.printStackTrace();
   ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
   return img;
  } finally {
   rgbOutput = null;
   rgbInput = null;
  }

 }

 /***************************************************************************
  * 缩小图片的方法
  */
 public static Image zoomOutImage(Image img) {

  int[] rgbOutput = null;
  int width = 0, height = 0;
  int[] rgbInput = null;
  try {
   width = img.getWidth();
   height = img.getHeight();
   // System.out.println(width >> 1);
   // System.out.println(height >> 1);
   rgbInput = new int[width * height];
   rgbOutput = new int[(width >> 1) * (height >> 1)];
   img.getRGB(rgbInput, 0, width, 0, 0, width, height);
   int i, j, k, k1;
   k = 0;
   k1 = 0;
   // System.out.println(height);
   // System.out.println(width);
   for (i = 0; i < height; i++) {
    for (j = 0; j < width; j += 2) {
     if (i % 2 == 0) {
      if (k1 > width * height - 1
        || k > (width >> 1) * (height >> 1) - 1) {
       // System.out.println("k=" + k);
       // System.out.println("k1=" + k1);
       break;
      }
      rgbOutput[k] = rgbInput[k1];
      k++;
      k1 += 2;
      // System.out.println(k);
      // System.out.println(k1);
     } else {
      k1 += 2;

     }
    }
   }

   return Image.createRGBImage(rgbOutput, width >> 1, height >> 1,
     true);
  } catch (OutOfMemoryError e) {
   // e.printStackTrace();
   ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
   return img;
  } finally {
   rgbOutput = null;
   rgbInput = null;
  }

 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值