将Java image对象转换成PNG格式字节数组 |
| 作者:不详 来源:本站整理 发布时间:2005-11-29 13:01:15 |
|
turn pngBytes; } /** * Creates an array of bytes that is the PNG equivalent of the current image. * Alpha encoding is determined by its setting in the constructor. * * @return an array of bytes, or null if there was a problem */ public byte[] pngEncode() { return pngEncode( encodeAlpha ); } /** * Set the alpha encoding on or off. * * @param encodeAlpha false=no, true=yes */ public void setEncodeAlpha( boolean encodeAlpha ) { this.encodeAlpha = encodeAlpha; } /** * Retrieve alpha encoding status. * * @return boolean false=no, true=yes */ public boolean getEncodeAlpha() { return encodeAlpha; } /** * Set the filter to use * * @param whichFilter from constant list */ public void setFilter( int whichFilter ) { this.filter = FILTER_NONE; if ( whichFilter <= FILTER_LAST ) { this.filter = whichFilter; } } /** * Retrieve filtering scheme * * @return int (see constant list) */ public int getFilter() { return filter; } /** * Set the compression level to use * * @param level 0 through 9 */ public void setCompressionLevel( int level ) { if ( level >= 0 && level <= 9) { this.compressionLevel = level; } } /** * Retrieve compression level * * @return int in range 0-9 */ public int getCompressionLevel() { return compressionLevel; } /** * Increase or decrease the length of a byte array. * * @param array The original array. * @param newLength The length you wish the new array to have. * @return Array of newly desired length. If shorter than the * original, the trailing elements are truncated. */ protected byte[] resizeByteArray( byte[] array, int newLength ) { byte[] newArray = new byte[newLength]; int oldLength = array.length; System.arraycopy( array, 0, newArray, 0, Math.min( oldLength, newLength ) ); return newArray; } /** * Write an array of bytes into the pngBytes array. * Note: This routine has the side effect of updating * maxPos, the largest element written in the array. * The array is resized by 1000 bytes or the length * of the data to be written, whichever is larger. * * @param data The data to be written into pngBytes. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. */ protected int writeBytes( byte[] data, int offset ) { maxPos = Math.max( maxPos, offset + data.length ); if (data.length + offset > pngBytes.length) { pngBytes = resizeByteArray( pngBytes, pngBytes.length + Math.max( 1000, data.length ) ); } System.arraycopy( data, 0, pngBytes, offset, data.length ); return offset + data.length; } /** * Write an array of bytes into the pngBytes array, specifying number of bytes to write. * Note: This routine has the side effect of updating * maxPos, the largest element written in the array. * The array is resized by 1000 bytes or the length * of the data to be written, whichever is larger. * * @param data The data to be written into pngBytes. * @param nBytes The number of bytes to be written. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. */ protected int writeBytes( byte[] data, int nBytes, int offset ) { maxPos = Math.max( maxPos, offset + nBytes ); if (nBytes + offset > pngBytes.length) { pngBytes = resizeByteArray( pngBytes, pngBytes.length + Math.max( 1000, nBytes ) ); } System.arraycopy( data, 0, pngBytes, offset, nBytes ); return offset + nBytes; } /** * Write a two-byte integer into the pngBytes array at a given position. * * @param n The integer to be written into pngBytes. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. */ protected int writeInt2( int n, int offset ) { byte[] temp = { (byte)((n >> 8) & 0xff), (byte) (n & 0xff) }; return writeBytes( temp, offset ); } /** * Write a four-byte integer into the pngBytes array at a given position. * * @param n The integer to be written into pngBytes. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. */ protected int writeInt4( int n, int offset ) { byte[] temp = { (byte)((n >> 24) & 0xff), (byte) ((n >> 16) & 0xff ), (byte) ((n >> 8) & 0xff ), (byte) ( n & 0xff ) }; return writeBytes( temp, offset ); } /** * Write a single byte into the pngBytes array at a given position. * * @param n The integer to be written into pngBytes. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. */ protected int writeByte( int b, int offset ) { byte[] temp = { (byte) b }; return writeBytes( temp, offset ); } /** * Write a string into the pngBytes array at a given position. * This uses the getBytes method, so the encoding used will * be its default. * * @param n The integer to be written into pngBytes. * @param offset The starting point to write to. * @return The next place to be written to in the pngBytes array. * @see java.lang.String#getBytes() */ protected int writeString( String s, int offset ) { return writeBytes( s.getBytes(), offset ); } /** * Writ |
| [] [返回上一页] [打 印] |
|
文章评论 |
