📄 image.java
字号:
} if ((width * height) > rgb.length) { throw new ArrayIndexOutOfBoundsException(); } return new Image(ImageDataFactory.getImageDataFactory(). createImmutableImageData(rgb, width, height, processAlpha)); } /** * Creates a new <code>Graphics</code> object that renders to this * image. This image * must be * mutable; it is illegal to call this method on an immutable image. * The mutability of an image may be tested * with the <code>isMutable()</code> method. * * <P>The newly created <code>Graphics</code> object has the * following properties: * </P> * <UL> * <LI>the destination is this <code>Image</code> object;</LI> * <LI>the clip region encompasses the entire <code>Image</code>;</LI> * <LI>the current color is black;</LI> * <LI>the font is the same as the font returned by * {@link Font#getDefaultFont() Font.getDefaultFont()};</LI> * <LI>the stroke style is {@link Graphics#SOLID SOLID}; and * </LI> * <LI>the origin of the coordinate system is located at the upper-left * corner of the Image.</LI> * </UL> * * <P>The lifetime of <code>Graphics</code> objects created using * this method is * indefinite. They may be used at any time, by any thread.</P> * * @return a <code>Graphics</code> object with this image as its destination * @throws IllegalStateException if the image is immutable */ public Graphics getGraphics() { if (isMutable()) { // SYNC NOTE: no locking necessary as getGraphics() only allocates // a new object return Graphics.getImageGraphics(this); } else { // SYNC NOTE: Not accessing any shared data, no locking necessary throw new IllegalStateException(); } } /** * Gets the width of the image in pixels. The value returned * must reflect the actual width of the image when rendered. * @return width of the image */ public int getWidth() { return width; } /** * Gets the height of the image in pixels. The value returned * must reflect the actual height of the image when rendered. * @return height of the image */ public int getHeight() { return height; } /** * Check if this image is mutable. Mutable images can be modified by * rendering to them through a <code>Graphics</code> object * obtained from the * <code>getGraphics()</code> method of this object. * @return <code>true</code> if the image is mutable, * <code>false</code> otherwise */ public boolean isMutable() { return imageData.isMutable(); } /** * Obtains ARGB pixel data from the specified region of this image and * stores it in the provided array of integers. Each pixel value is * stored in <code>0xAARRGGBB</code> format, where the high-order * byte contains the * alpha channel and the remaining bytes contain color components for * red, green and blue, respectively. The alpha channel specifies the * opacity of the pixel, where a value of <code>0x00</code> * represents a pixel that * is fully transparent and a value of <code>0xFF</code> * represents a fully opaque * pixel. * * <p> The returned values are not guaranteed to be identical to values * from the original source, such as from * <code>createRGBImage</code> or from a PNG * image. Color values may be resampled to reflect the display * capabilities of the device (for example, red, green or blue pixels may * all be represented by the same gray value on a grayscale device). On * devices that do not support alpha blending, the alpha value will be * <code>0xFF</code> for opaque pixels and <code>0x00</code> for * all other pixels (see <a * href="#alpha">Alpha Processing</a> for further discussion.) On devices * that support alpha blending, alpha channel values may be resampled to * reflect the number of levels of semitransparency supported.</p> * * <p>The <code>scanlength</code> specifies the relative offset within the * array between the corresponding pixels of consecutive rows. In order * to prevent rows of stored pixels from overlapping, the absolute value * of <code>scanlength</code> must be greater than or equal to * <code>width</code>. Negative values of <code>scanlength</code> are * allowed. In all cases, this must result in every reference being * within the bounds of the <code>rgbData</code> array.</p> * * <p>Consider <code>P(a,b)</code> to be the value of the pixel * located at column <code>a</code> and row <code>b</code> of the * Image, where rows and columns are numbered downward from the * top starting at zero, and columns are numbered rightward from * the left starting at zero. This operation can then be defined * as:</p> * * <TABLE BORDER="2"> * <TR> * <TD ROWSPAN="1" COLSPAN="1"> * <pre><code> * rgbData[offset + (a - x) + (b - y) * scanlength] = P(a, b); * </code></pre> * </TD> * </TR> * </TABLE> * <p>for</p> * * <TABLE BORDER="2"> * <TR> * <TD ROWSPAN="1" COLSPAN="1"> * <pre><code> * x <= a < x + width * y <= b < y + height </code></pre> * </TD> * </TR> * </TABLE> * * <p>The source rectangle is required to not exceed the bounds of * the image. This means: </p> * <TABLE BORDER="2"> * <TR> * <TD ROWSPAN="1" COLSPAN="1"> * <pre><code> * x >= 0 * y >= 0 * x + width <= image width * y + height <= image height </code></pre> * </TD> * </TR> * </TABLE> * <p> * If any of these conditions is not met an * <code>IllegalArgumentException</code> is thrown. Otherwise, in * cases where <code>width <= 0</code> or <code>height <= 0</code>, * no exception is thrown, and no pixel data is copied to * <code>rgbData</code>.</p> * * @param rgbData an array of integers in which the ARGB pixel data is * stored * @param offset the index into the array where the first ARGB value * is stored * @param scanlength the relative offset in the array between * corresponding pixels in consecutive rows of the region * @param x the x-coordinate of the upper left corner of the region * @param y the y-coordinate of the upper left corner of the region * @param width the width of the region * @param height the height of the region * * @throws ArrayIndexOutOfBoundsException if the requested operation would * attempt to access an element in the <code>rgbData</code> array * whose index is either * negative or beyond its length (the contents of the array are unchanged) * * @throws IllegalArgumentException if the area being retrieved * exceeds the bounds of the source image * * @throws IllegalArgumentException if the absolute value of * <code>scanlength</code> is less than <code>width</code> * * @throws NullPointerException if <code>rgbData</code> is <code>null</code> * * @since MIDP 2.0 */ public void getRGB(int[] rgbData, int offset, int scanlength, int x, int y, int width, int height) { int img_width = imageData.getWidth(); int img_height = imageData.getHeight(); // see if absolute value of scanlength is greater than or // equal to width if (scanlength >= 0 && scanlength < width) { throw new IllegalArgumentException(); } else if (scanlength < 0 && (0 - scanlength) < width) { throw new IllegalArgumentException(); } else if((y < 0) || (x < 0) || (x + width > img_width) || (y + height > img_height)) { throw new IllegalArgumentException(); } else if (height < 0 || width < 0 ) { /* spec says noop in this case */ } else { // will throw a NullPointerException int buflen = rgbData.length; if (offset < 0 || offset + ((height - 1) * scanlength) + width > buflen || offset + ((height - 1) * scanlength) < 0) { throw new ArrayIndexOutOfBoundsException(); } imageData.getRGB(rgbData, offset, scanlength, x, y, width, height); } } /** * Returns <code>ImageData</code> associated with this * <code>Image</code>. * * @return The <code>ImageData </code> associated with this * <code>Image</code>. */ ImageData getImageData() { return imageData; } /** * Function to load an romized Image. * * @param imageDataArrayPtr native pointer to image data as Java int * @param imageDataArrayLength length of image data array * @return image created. Null if no romized image matches the id. */ static Image getRomizedImage(int imageDataArrayPtr, int imageDataArrayLength) { try { AbstractImageDataFactory f = ImageDataFactory.getImageDataFactory(); ImageData data = f.createImmutableImageData(imageDataArrayPtr, imageDataArrayLength); return new Image(data); } catch (IllegalArgumentException iae) { return null; } } /** * Creates an Immutable image from the given ImageData. * @param imageData <code>ImageData</code> instance to be used to * create new Image */ private Image(ImageData imageData) { this.imageData = imageData; this.width = imageData.getWidth(); this.height = imageData.getHeight(); } /** * Resize Image optionally saving its content clipped according * to the new geometry * * @param width new width of the Image * @param height new height of the Image * @param keepContent keep current content of the image * binded to the (0, 0) of the resized image and clipped * according to the new image dimensions */ void resize(int width, int height, boolean keepContent) { if (!imageData.isMutable() || width <= 0 || height <= 0) { throw new IllegalArgumentException(); } // IMPL_NOTE: In the case content is not kept it is possible // to resize the image more efficiently, especially for the // case of rotation, when the memory reallocation is not needed. // However, now there are no scenarios when resize is needed // without content saving. Image newImage = createImage(width, height); synchronized(this) { if (keepContent) { Graphics.getImageGraphics(newImage).render(this, 0, 0, Graphics.TOP | Graphics.LEFT); } this.width = width; this.height = height; imageData = newImage.getImageData(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -