I used to collect this function on the internet, regardless of the effect or performance. I only think it is very useful.
It took a long time, but unfortunately I don't know who it is. Thank you for the author.
This function uses the getrgb () function of midp2.0, which is efficient. Basically, there is nothing to optimize.
- Public static image zoomimage (image SRC, int desw, int Desh ){
- Image desimg = NULL;
- Int srcw = SRC. getwidth (); // Original Image Width
- Int srch = SRC. getheight (); // The height of the original image.
- Int [] srcbuf = new int [srcw * srch]; // cache the pixel information of the original image.
- SRC. getrgb (srcbuf, 0, srcw, 0, 0, srcw, srch );
- // Calculate the interpolation table
- 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 ++) {/* vertical */
- 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 ++) {/* horizontal direction */
- Tabx [dB] = (short) Sb;
- Tems + = srcw;
- Temd + = desw;
- If (TEMS> distance ){
- Tems-= distance;
- SB ++;
- }
- If (temd> distance ){
- Temd-= distance;
- DB ++;
- }
- }
- // Generate the image pixel Buf after zoom-in and zoom-out
- 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;
- }
- // Generate an image
- Desimg = image. creatergbimage (desbuf, desw, Desh, false );
- Return desimg;
- }