前段時間接觸了一些數字圖像處理的問題
圖像的變形變換
看下面的一個圖片放大的例子
獲取Image圖片像素信息
標准的midp
g = image
DirectGraphics dg = DirectUtils
dg
參數介紹
short[] pixels
int offset
int scanlength
int x
int y
int width
int height
int format
想具體了解Nokia SDK的信息
使用像素信息數組生成Image圖片
image = Image
g = image
DirectGraphics dg = DirectUtils
dg
short[] pixels
boolean transparency
int offset
int scanlength
int x
int y
int width
int height
int manipulation
int format
下面開始介紹具體的算法
/*********************************
* @todo 圖片放大縮小
* @param srcImg 原始圖片
* @param desW 變化後圖片的寬
* @param desH 變化後圖片的高
* @return 處理後的圖片
*********************************/
private Image ZoomImage(Image srcImg
int srcW = srcImg
int srcH = srcImg
short[] srcBuf = new short[srcW * srcH]; //原始圖片像素信息緩存
//srcBuf獲取圖片像素信息
Image desImg = Image
if (srcImg
DirectUtils
getPixels(srcBuf
} else { /*如果是非可變圖像*/
desImg
DirectUtils
getPixels(srcBuf
}
//計算插值表
short[] tabY = new short[desH];
short[] tabX = new short[desW];
int sb =
int db =
int tems =
int temd =
int distance = srcH > desH ? srcH : desH;
for (int i =
tabY[db] = (short) sb;
tems += srcH;
temd += desH;
if (tems > distance) {
tems
sb++;
}
if (temd > distance) {
temd
db++;
}
}
sb =
db =
tems =
temd =
distance = srcW > desW ? srcW : desW;
for (int i =
tabX[db] = (short) sb;
tems += srcW;
temd += desW;
if (tems > distance) {
tems
sb++;
}
if (temd > distance) {
temd
db++;
}
}
short[] desBuf = new short[desW * desH];
int dx =
int dy =
int sx =
int sy =
int oldy =
for (int i =
if (oldy == tabY[i]) {
System
} else {
dx =
for (int j =
desBuf[dy + dx] = srcBuf[sy + tabX[j]];
dx++;
}
sy += (tabY[i]
}
oldy = tabY[i];
dy += desW;
}
//生成圖片
desImg = Image
DirectUtils
drawPixels(desBuf
return desImg;
}
首先看函數的頭兩句
int srcW = srcImg
int srcH = srcImg
接下來一句我們要定義一個short型數組
short[] srcBuf = new short[srcW * srcH];
再下來一段
Image desImg = Image
if (srcImg
DirectUtils
getPixels(srcBuf
} else { /*如果是非可變圖像*/
desImg
DirectUtils
getPixels(srcBuf
}
再往下就是縮放算法的重點
大家看下面這個水平的
|
如果要將這個表格放大成
distance = srcW > desW ? srcW : desW;
for (int i =
tabX[db] = (short) sb;
tems += srcW;
temd += desW;
if (tems > distance) {
tems
sb++;
}
if (temd > distance) {
temd
db++;
}
}
很明顯原始表格寬度srcW =
接下來進入for循環
| i| tabX賦值操作| tems | temd| sb | db |
|
|
|
|
|
|
|
有此得到放大後
|
例如
| 紅 | 綠 | 蘭 | 紫 | | 紅 | 綠 | 綠 | 蘭 | 紫 | 紫 |
同樣
有了
short[] desBuf = new short[desW * desH];
int dx =
int dy =
int sx =
int sy =
int oldy =
for (int i =
if (oldy == tabY[i]) { /**********情況一**********/
System
} else { /**********情況二**********/
dx =
for (int j =
desBuf[dy + dx] = srcBuf[sy + tabX[j]];
dx++;
}
sy += (tabY[i]
}
oldy = tabY[i];
dy += desW;
}
desBuf是用來保存放大縮小後的圖像數據
在循環中會判斷是否oldy 等於 tabY[i]
算法演示過程如下
| i|oldy|tabY[i]|運算情況|
|
|
|
|
|
|
然後我們用desBuf生成最終放大或縮小後的圖片
desImg = Image
DirectUtils
drawPixels(desBuf
return desImg;
最後要說明一點的是
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26482.html