public static Image ZoomImage(Image src, int desW, int desH) {
Image desImg = null;
int srcW = src.getWidth(); // 原始圖像寬
int srcH = src.getHeight(); // 原始圖像高
int[] srcBuf = new int[srcW * srcH]; // 原始圖片像素信息緩存
src.getRGB(srcBuf, 0, srcW, 0, 0, srcW, srcH);
// 計算插值表
int[] tabY = new int[desH];
int[] tabX = new int[desW];
int sb = 0;
int db = 0;
int tems = 0;
int temd = 0;
int distance = srcH > desH ? srcH : desH;
for (int i = 0; i <= distance; i++) { /* 垂直方向 */
tabY[db] = 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[] desBuf = new int[desW * desH];
int dx = 0;
int dy = 0;
int sy = 0;
int oldy = -1;
for (int i = 0; i < desH; i++) {
if (oldy == tabY[i]) {
System.arraycopy(desBuf, dy - desW, desBuf, dy, desW);
} else {
dx = 0;
for (int j = 0; j < desW; j++) {
desBuf[dy + dx] = srcBuf[sy + tabX[j]];
dx++;
}
sy += (tabY[i] - oldy) * srcW;
}
oldy = tabY[i];
dy += desW;
}
// 生成圖片
desImg = Image.createRGBImage(desBuf, desW, desH, false);
return desImg;
}
這個函數是我以前在網上搜羅到的,且不談效果,性能什麼的。只覺得它非常好用。
用了很長時間,可惜不知道是誰。真要謝謝這位作者了。
這個函數使用了midp2.0的getRGB()函數,效率不錯,基本上沒什麼可優化的了。
此外,下面再提供一個midp1.0下可用的縮放函數,它是使用可變圖片實現的。可惜這個函數也不是我寫的。
轉載於kobjects。不過性能確實比較差,畢竟要畫那麼多點嘛,跟處理圖片數據的方法是沒有可比性的。
1 /* Copyright (c) 2002,2003, Stefan Haustein, Oberhausen, Rhld., Germany
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the “Software”), to deal
5 * in the Software without restriction, including without limitation the rights
6 * to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copIEs of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copIEs or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19 * IN THE SOFTWARE. */
20
21 package org.kobjects.lcdui;
22
23
24 import Javax.microedition.lcdui.*;
25
26
27 /** This class provides a single static method that allows to scale an image */
28
29
30 public class ScaleImage {
31
32 /**
33 * Creates a new, scaled version of the given image.
34 *
35 * @param src: The source image
36 * @param dstW: The destination (scaled) image width
37 * @param dstH: The destination (scaled) image height
38 * @return Image: A new Image object with the given width and height.
39 */
40
41 public static Image scaleImage (Image src, int dstW, int dstH) {
42 int srcW = src.getWidth();
43 int srcH = src.getHeight();
44
45 Image tmp = Image.createImage(dstW, srcH);
46 Graphics g = tmp.getGraphics();
47
48 int delta = (srcW << 16) / dstW;
49 int pos = delta/2;
50
51 for (int x = 0; x < dstW; x++) {
52 g.setClip(x, 0, 1, srcH);
53 g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
54 pos += delta;
55 }
56
57 Image dst = Image.createImage(dstW, dstH);
58 g = dst.getGraphics();
59
60 delta = (srcH << 16) / dstH;
61 pos = delta/2;
62
63 for (int y = 0; y < dstH; y++) {
64 g.setClip(0, y, dstW, 1);
65 g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
66 pos += delta;
67 }
68
69 return dst;
70 }
71
72
73 }
它們的使用方法都是一目了然,提供原始圖片對象以及目標寬度跟高度,它就生成新的圖片,在成像效果上,感覺都差不多,我覺得第一個方法更好。
在J2ME平台下,也沒有必要去最求效果的極致,夠用就好。再次感謝以上兩個函數的作者。