SimpleImageTrans.java
资源名称:J2ME.rar [点击查看]
上传用户:sygbg6
上传日期:2020-12-29
资源大小:4884k
文件大小:2k
源码类别:
游戏
开发平台:
Java
- package cn.com.imageanalyze.util;
- import javax.microedition.lcdui.Image;
- public class SimpleImageTrans {
- /**
- * 线性插值算法,缩放图片
- * @param src
- * @param desW
- * @param desH
- * @return
- */
- public static int[] trans(Image src,int desW,int desH){
- // 计算差值表
- int srcH = src.getHeight();
- int srcW = src.getWidth();
- int srcBuff[] = new int[srcH * srcW];
- src.getRGB(srcBuff, 0, srcW, 0, 0, srcW, srcH);
- int sb = 0;
- int db = 0;
- int tems = 0;
- int temd = 0;
- int[] tabx = new int[desW];
- int[] taby = new int[desH];
- int distance = srcH > desH ? srcH : desH;
- for (int i = 0; i <= distance; i++) { /*垂直方向*/
- taby[db] = (short) sb;
- tems += srcH;
- temd += desH;
- if (tems > distance) {
- tems -= distance;
- sb++;
- }
- if (temd > distance) {
- temd -= distance;
- db++;
- }
- }
- sb = 0;
- db = 0;
- tems = 0;
- temd = 0;
- distance = srcW > desW ? srcW : desW;
- for (int i = 0; i <= distance; i++) { /*水平方向*/
- tabx[db] = (short) sb;
- tems += srcW;
- temd += desW;
- if (tems > distance) {
- tems -= distance;
- sb++;
- }
- if (temd > distance) {
- temd -= distance;
- db++;
- }
- }
- // 生成放大缩小后图形像素buf
- int[] desBuff = new int[desW * desH];
- int dx = 0;
- int dy = 0;
- int sx = 0;
- int sy = 0;
- int oldy = -1;
- for (int i = 0; i < desH; i++) {
- if (oldy == taby[i]) {
- System.arraycopy(desBuff, dy - desW, desBuff, dy, desW);
- } else {
- dx = 0;
- for (int j = 0; j < desW; j++) {
- desBuff[dy + dx] = srcBuff[sy + tabx[j]];
- dx++;
- }
- sy += (taby[i] - oldy) * srcW;
- }
- oldy = taby[i];
- dy += desW;
- }
- return desBuff;
- // return null;
- }
- }