Zoom function based on MIDP2.0 implementation picture

xiaoxiao2021-03-06  42

Package com.j2medev.image;

Import javax.microedition.lcdui. *;

Public class imageutil {

// Fixed Point Constants Private Static Final Int fp_shift = 13;

Private static final int fp_one = 1 << fp_shift;

Private static final int fp_half = 1 << (fp_shift - 1);

// resampling modes - valid values ​​for the mode parameter of resizeImage () // any other value will default to MODE_BOX_FILTER because of the way the // conditionals are set in resizeImage () public static final int MODE_POINT_SAMPLE = 0;

Public static final int mode_box_filter = 1;

/ ** * getPixels Wrapper for pixel grabbing techniques. I separated this step * into it's own function so that other APIs (Nokia, Motorola, Siemens, * etc.) can easily substitute the MIDP 2.0 API (Image.getRGB ()). * @Param src * The source image whose pixels we are grabbing. * @Return an int Array Containing The Pixels in 32 bit argb format. * / Int [] getpixels (image src) {int w = src.getwidth (); INT H = src.getHeight (); int [] pixels = new int [w * h]; src.getrgb (Pixels, 0, W, 0, 0, W, H); Return Pixels;

/ ** * drawPixels Wrapper for pixel drawing function. I separated this step into * it's own function so that other APIs (Nokia, Motorola, Siemens, etc.) can * easily substitute the MIDP 2.0 API (Image.createRGBImage ()). * * @param pixels * int array containing the pixels in 32 bit ARGB format. * @param w * The width of the image to be created. * @param h * The height of the image to be created. This parameter is * actually superfluous, because it must equal pixels.length / w. * @return The image created from the pixel array. * / Image drawPixels (int [] pixels, int w, int h) {return Image.createRGBImage (pixels, w, h ,} / ** * ResizEIMAGE GETS A Source Image Along with new size for it and resizes * it. * @Param src * the source image. * @Param destw * The new width for the destination image. * @ Param desth * .. The new heigth for the destination image * @param mode * A flag indicating what type of resizing we want to do It * currently supports two type: MODE_POINT_SAMPLE - point sampled * resizing, and MODE_BOX_FILTER - box filtered resizing * (default). * @Return The Resized Image. * / Image ResizeImage (IMAGE SRC, INT DESTW, INT DESTH, INT MODE) {Int srcw = src.getwidth (); int srch = src.getHeight ();

// create pixel arrays int [] destPixels = new int [destW * destH]; // array to hold destination // pixels int [] srcPixels = getPixels (src); // array with source's pixelsif (mode == MODE_POINT_SAMPLE) { // Simple point the destination pixels, Find the matching pixel on // the source and use trice for (int desty = 0; DESTY

int [] TMPPIXELS = New Int [destw * srch]; // Temporary Buffer for the // Horizontal Resampling // STEP

// Variables to Perform; // Color Extracted from Source Int A, R, G, B; // Separate Channels of the Color Int Count; // Number of Pixels Sampled for Calculating The average

// the resampling will be separated into 2 steps for simplicity // the first step will keep the same height and just stretch the // picture horizontally // the second step will take the intermediate result and stretch it // vertically // horizontal resampling For (int y = 0; y > FP_SHIFT; // Calculate // Beginning OF // Sample Int Srcx2 = (DESTX 1) * Ratiow >> fp_shift; // Calculate / / END OF // Sample

// now loop from srcx to srcx2 and add up the value for // each channel DO {argb = srcpixels [srcx y * srcw]; A = ((argb & 0xff000000) >> 24); // alpha channel R = ((Argb & 0x00FF0000) >> 16); // red channel G = ((argb & 0x0000FF00) >> 8); // Green Channel B = (Argb & 0x000000FF); // Blue Channel Count; // count The Pixel srcx; //move on to the next pixel} while (srcx <= srcx2 && srcx y * srcw

// recreate color from the aveled channel channel and place it // INTO the TEMPORY BUFFER TMPPIXELS [DESTX Y * DESTW] = ((a << 24) | (r << 16) | (g << 8) | b) }}

// Vertical Resampling of the Temporary Buffer (Which Has Been // Horizontally ") System.out.Println (" Vertical Resampling ... "; for (int x = 0; x > FP_SHIFT; // Calculate // Beginning OF // Sample Int Srcy2 = (DESTY 1) * Ratioh >> fp_shift; // Calculate // end of // Sample

// Now loop from srcy to srcy2 and add up the value for // each channel DO {argb = tmpixels [x srcy * destw]; A = ((argb & 0xff000000) >> 24); // Alpha Channel R = ((Argb & 0x00FF0000) >> 16); // red channel G = ((argb & 0x0000FF00) >> 8); // Green Channel B = (Argb & 0x000000FF); // Blue Channel Count; // count The Pixel srcy; //move on to the next pixel} while (srcy <= srcy2 && x srcy * destw 255)? 255: a; r / = count; r = (r> 255)? 255: r; g / = count; g = (g> 255)? 255: g; b / = count ; B = (B> 255)? 255: B;

// recreate color from the aveled channels and place it // INTO THE DESTINATION BUFFER DESTPIXELS [X DESTY * DESTW] = ((a << 24) | (r << 16) | (g << 8) | b) }}}

// Return A New Image Created from The Destination Pixel Buffer Return Drawpixels (Destpixels, Destw, Desh);

}

Interested netizens can study it carefully. I have prepared a picture and then wrote a test MIDlet. The following is a comparison.

/ * * Created on 2004-12-24 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates * / package com.j2medev.image; import java.io.IOException ;

import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.Image; import javax.microedition.midlet.MIDlet; import javax.microedition. MIDlet.midletStateChangeException;

Public Class Testmidlet Extends MIDlet {Private Display Display;

PRIVATE FORM FORM;

Protected void Startapp () throws midletStateChangeException {display = display.getdisplay (this); image srcimage = null; try {srcimage = image.createImage ("/ welcome.png");

} catch (IOEXCEPTION E) {} imageutil iu = new imageutil (); form = new form ("image"); int width = form.getWidth (); int Height = form.getHEight (); Image destimage = u.resizeImeimage (srcimage, width, height, imageutil.mode_point_sample; form.append (destimage); Display.SetCurrent (Form);

}

protected void pauseapp () {}

Protected Void DestroyApp (Boolean Arg0) throws midletStateChangeException {

}

}

转载请注明原文地址:https://www.9cbs.com/read-57687.html

New Post(0)