📄 image.java
字号:
* @param imageData the array of image data in a supported image format * @param imageOffset the offset of the start of the data in the array * @param imageLength the length of the data in the array * * @return the created image * @throws ArrayIndexOutOfBoundsException if imageOffset and imageLength * specify an invalid range * @throws NullPointerException if imageData is null * @throws IllegalArgumentException if imageData is incorrectly formatted or * otherwise cannot be decoded */ public static Image createImage(byte[] imageData, int imageOffset, int imageLength) { if (imageOffset < 0 || imageOffset >= imageData.length || imageLength < 0 || imageOffset + imageLength > imageData.length) { throw new ArrayIndexOutOfBoundsException(); } // SYNC NOTE: Not accessing any shared data, no locking necessary return new ImmutableImage(imageData, imageOffset, imageLength); } /** * <p>Creates a new Graphics 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 isMutable() method.</P> * * <P>The newly created Graphics object has the following properties: * </P> * <UL> * <LI>the destination is this Image object;</LI> * <LI>the clip region encompasses the entire Image;</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 Graphics objects created using this method is * indefinite. They may be used at any time, by any thread.</P> * * @return a Graphics object with this image as its destination * @throws IllegalStateException if the image is immutable */ public Graphics getGraphics() { // SYNC NOTE: Not accessing any shared data, no locking necessary throw new IllegalStateException(); } /** * Gets the width of the image in pixels. * @return width of the image */ public int getWidth() { // SYNC NOTE: return of atomic value, no locking necessary return width; } /** * Gets the height of the image in pixels. * @return height of the image */ public int getHeight() { // SYNC NOTE: return of atomic value, no locking necessary return height; } /** * Check if this image is mutable. Mutable images can be modified by * rendering to them through a Graphics object obtained from * the getGraphics() * method of this object. * @return true if the image is mutable, false otherwise */ public boolean isMutable() { // SYNC NOTE: return of atomic value, no locking necessary return false; } /** * The width, height of this Image */ int width, height; /** * The byte[] containing the image data */ private byte[] data;}/** * A special mutable Image subclass */class MutableImage extends Image { /** * Create a new mutable Image * * @param width The width of the mutable Image * @param height The height of the mutable Image */ MutableImage(int width, int height) { this.width = width; this.height = height; createMutableImage(width, height); } /** * Determine if this Image is mutable * * @return boolean Always returns true */ public boolean isMutable() { // SYNC NOTE: return of atomic value, no locking necessary return true; } /** * Get the Graphics context for this Image * * @return Graphics The Graphics context for this Image */ public Graphics getGraphics() { // SYNC NOTE: no locking necessary as getGraphics() only allocates // a new object return Graphics.getGraphics(this); } /** * Create a mutable image * * @param width The width of the new mutable image * @param height The height of the new mutable image */ private native void createMutableImage(int width, int height);}/** * A special immutable Image subclass */class ImmutableImage extends Image { /** * Create a new immutable Image with the given Image * * @param img The Image to use to create an immutable copy */ ImmutableImage(Image img) { this.width = img.width; this.height = img.height; createImmutableCopy(width, height, img); } /** * Create an immutable Image with the given byte data * * @param imageData The byte[] image data * @param imageOffset The offset in the array marking the start * of the image data * @param imageLength The length of the image data in the array */ ImmutableImage(byte[] imageData, int imageOffset, int imageLength) { if (imageOffset < 0 || imageOffset >= imageData.length || imageLength < 0 || imageOffset + imageLength > imageData.length) { throw new ArrayIndexOutOfBoundsException(); } decodeImage(imageData, imageOffset, imageLength); } /** * Create an immutable Image from the given file name * * @param str A String holding a file name to load the image data from */ ImmutableImage(String str) throws java.io.IOException { /* * allocate an array and read in the bits using * Class.getResourceAsStream(name); */ InputStream is = null; int blocksize = 4096; // the size of blocks to read and allocate is = getClass().getResourceAsStream(str); if (is == null) { throw new java.io.IOException(); } else { /* * Allocate an array assuming available is correct. * Only reading an EOF is the real end of file * so allocate an extra byte to read the EOF into. * If available is incorrect, increase the buffer * size and keep reading. */ int l = is.available(); byte[] buffer = new byte[l+1]; int length = 0; // TBD: Guard against an implementation with incorrect available while ((l = is.read(buffer, length, buffer.length-length)) != -1) { length += l; if (length == buffer.length) { byte[] b = new byte[buffer.length + blocksize]; System.arraycopy(buffer, 0, b, 0, length); buffer = b; } } decodeImage(buffer, 0, length); is.close(); } } /** * Native function to create an immutable copy of an image * * @param width The width of the new Image * @param height The height of the new Image * @param img The Image to make a copy of */ private native void createImmutableCopy(int width, int height, Image img); /** * Native function to decode an Image from a byte array * * @param inputData The byte array image data * @param offset The start of the image data within the byte array * @param length The length of the image data in the byte array */ private native void decodeImage(byte[] inputData, int offset, int length);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -