如何在J2ME里旋转显示图像

J2ME MIDP2.0包括自己的方法:

public static Image createImage(Image image,
                                int x,
                                int y,
                                int width,
                                int height,
                                int transform)

其中:transform就可以指定要做的操作,比如水平镜像,转90度等

本例的代码取自我写的上阳图像处理演示程序(ImageAlbum),自己实现的方法,提供另一种思路。大家应该尽量使用系统内置的API

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

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

 /***************************************************************************
  *
  * 旋转90度的方法
  */
 public static Image turn90(Image img) {

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

   width = img.getWidth();
   height = img.getHeight();
   rgbInput = new int[width * height];
   // rgbOutput = new int[width * height];
   img.getRGB(rgbInput, 0, width, 0, 0, width, height);
   int i, j, k;
   k = 0;
   tempArr = new int[height][width];
   for (i = 0; i < height; i++)
    for (j = 0; j < width; j++)
     tempArr[i][j] = rgbInput[k++];

   rgbInput = null;// 显式地设置为空值,告诉系统可以垃圾回收
   k = 0;
   tempArr1 = new int[width][height];
   for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
     tempArr1[j][i] = tempArr[i][j];
    }
   }
   tempArr = null;// 显式地设置为空值,告诉系统可以垃圾回收
   // 不调用这个方法,不是顺时针90度
   Image result = verticalMirror(tempArr1, height, width);
   // k = 0;
   // for (i = 0; i < width; i++) {
   // for (j = 0; j < height; j++) {
   // rgbOutput[k] = tempArr1[i][j];
   // k++;
   // }
   // }
   // tempArr1 = null;// 显式地设置为空值,告诉系统可以垃圾回收
   // // 不调用这个方法,不是顺时针90度
   // Image result = verticalMirror(rgbOutput, height, width);
   if (result == null)
    // 水平镜像的时候出问题,内存不够了,返回原来的图,不做修改
    return img;
   else
    return result;
   // return Image.createRGBImage(rgbOutput, height, width, true);

  } catch (OutOfMemoryError e) {
   // e.printStackTrace();
   ImageAlbum.showAlert("图像尺寸太大,不能完成此操作.");
   return img;
  } finally {
  }

 }

 /***************************************************************************
  *
  * 旋转180度的方法
  */
 public static Image turn180(Image img) {

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

   width = img.getWidth();
   height = img.getHeight();
   rgbInput = new int[width * height];
   rgbOutput = new int[width * height];
   img.getRGB(rgbInput, 0, width, 0, 0, width, height);
   int i, j, k;
   k = 0;
   tempArr = new int[height][width];
   for (i = 0; i < height; i++)
    for (j = 0; j < width; j++)
     tempArr[i][j] = rgbInput[k++];
   rgbInput = null;// 显式地设置为空值,告诉系统可以垃圾回收
   k = 0;
   for (i = height - 1; i >= 0; i--) {
    for (j = width - 1; j >= 0; j--) {
     rgbOutput[k] = tempArr[i][j];
     k++;
    }
   }
   tempArr = null;// 显式地设置为空值,告诉系统可以垃圾回收
   // return img;
   return Image.createRGBImage(rgbOutput, width, height, true);

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

  }

 }

 /***************************************************************************
  *
  * 垂直镜像的私有方法,被turn90调用
  */
 private static Image verticalMirror(int[][] tempArr, int width, int height) {

  int[] rgbOutput = null;
  int[][] tempArr1 = null;
  try {
   rgbOutput = new int[width * height];
   int i, j, k;
   k = 0;
   tempArr1 = new int[height][width];
   for (i = 0; i < height; i++)
    for (j = 0; j < width; j++) {
     tempArr1[i][width - 1 - j] = tempArr[i][j];
    }
   tempArr = null;// 显式地设置为空值,告诉系统可以垃圾回收
   k = 0;
   for (i = 0; i < height; i++)
    for (j = 0; j < width; j++) {
     rgbOutput[k] = tempArr1[i][j];
     k++;

    }
   tempArr1 = null;// 显式地设置为空值,告诉系统可以垃圾回收
   // return img;
   return Image.createRGBImage(rgbOutput, width, height, true);
  } catch (OutOfMemoryError e) {
   e.printStackTrace();
   return null;
  } finally {
   rgbOutput = null;

  }

 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值