📄 pngencoder.java~94~
字号:
package dyimg;import java.io.*;import java.util.zip.*;import java.awt.Image;import java.awt.image.PixelGrabber;public class PngEncoder{ public static final boolean ENCODE_ALPHA = true; public static final boolean NO_ALPHA = false; public static final int FILTER_NONE = 0; public static final int FILTER_SUB = 1; public static final int FILTER_UP = 2; public static final int FILTER_LAST = 2; private byte m_pngBytes[]; private byte m_priorRow[]; private byte m_leftBytes[]; private Image m_image; private int m_nWidth; private int m_nHeight; private int m_nBytePos; private int m_nMaxPos; private int m_nHdrPos; private int m_nDataPos; private int m_nEndPos; private CRC32 m_crc; private long m_lCrcValue; private boolean m_bEncodeAlpha; private int m_nBytesPerPixel; private int m_nCompressionLevel; private int m_nFilter=0; public PngEncoder(Image image) { this(image, false, 0); } /** * 构造方法 * @param image为要转化的图片文件 * @param flag为是否要进行Alpha过滤 * @param i为过滤器的设置 * @param j为压缩率 **/ public PngEncoder(Image image, boolean flag, int i) { m_crc = new CRC32(); this.m_image = image; m_bEncodeAlpha = flag; if (i >= 0 && i <= 9) m_nCompressionLevel = i; } /** * 生成PNG格式的byte数组 * @param flag为是否进行Alpha过滤 */ public byte[] pngEncode(boolean flag) { byte ab[] = { -119, 80, 78, 71, 13, 10, 26, 10 }; if (m_image == null) return null; m_nWidth = m_image.getWidth(null); m_nHeight = m_image.getHeight(null); m_pngBytes = new byte[(m_nWidth + 1) * m_nHeight * 3 + 200]; m_nMaxPos = 0; m_nBytePos = writeBytes(ab, 0); m_nHdrPos = m_nBytePos; writeHeader(); m_nDataPos =m_nBytePos; if (writeImageData()) { writeEnd(); m_pngBytes = resizeByteArray(m_pngBytes, m_nMaxPos); } else m_pngBytes = null; return m_pngBytes; } /** * 生成PNG格式的byte数组 */ public byte[] pngEncode() { return pngEncode(m_bEncodeAlpha); } /**设置是否进行Alpha过滤**/ public void setEncodeAlpha(boolean flag) { m_bEncodeAlpha = flag; } /**取得当前的Alpha设置**/ public boolean getEncodeAlpha() { return m_bEncodeAlpha; } /**设置压缩等级**/ public void setCompressionLevel(int i) { if (i >= 0 && i <= 9) m_nCompressionLevel = i; } /**取得压缩等级**/ public int getCompressionLevel() { return m_nCompressionLevel; } /**设置过滤等级**/ public void setFilter(int i) { m_nFilter = 0; if (i <= 2) m_nFilter = i; } /**取得过滤等级**/ public int getFilter() { return m_nFilter; } /** * 当过滤等级为1的时候, * 使用的过滤器 */ protected void filterSub(byte ab[], int i1, int j1) { int i2 = m_nBytesPerPixel; int j2 = i1 + i2; int k2 = j1 * m_nBytesPerPixel; int i3 = i2; int j3 = 0; int k1 = j2; while (k1<ab.length) { if (k1 >= i1 + k2) m_leftBytes[i3] = ab[k1]; ab[k1] = (byte)((ab[k1] - m_leftBytes[j3]) % 256); i3 = (i3 + 1) % 15; j3 = (j3 + 1) % 15; k1++; } } /** * 当过滤器为2的时候,使用的过滤器 */ protected void filterUp(byte ab[], int i1, int j) { int i2 = j * m_nBytesPerPixel; int k = 0; byte b=0; while (i1+k<ab.length) { if (k >= i2) b = ab[i1 + k]; ab[i1 + k] = (byte)((ab[i1 + k] - m_priorRow[k]) % 256); m_priorRow[k] = b; k++; } } /**改变数组的大小**/ private byte[] resizeByteArray(byte ab1[], int i) { byte ab2[] = new byte[i]; int j = ab1.length; System.arraycopy(ab1, 0, ab2, 0, Math.min(j, i)); return ab2; } /**往m_pngBytes里头在i处写入数组ab**/ private int writeBytes(byte ab[], int i) { m_nMaxPos = Math.max(m_nMaxPos, i + ab.length); if (ab.length + i > m_pngBytes.length) m_pngBytes = resizeByteArray(m_pngBytes, m_pngBytes.length + Math.max(1000, ab.length)); System.arraycopy(ab, 0, m_pngBytes, i, ab.length); return i + ab.length; } /**往m_pngBytes里头在i处写入数组ab,写的长度为j**/ private int writeBytes(byte ab[], int i, int j) { m_nMaxPos = Math.max(m_nMaxPos, j + i); if (i + j > m_pngBytes.length) m_pngBytes = resizeByteArray(m_pngBytes, m_pngBytes.length + Math.max(1000, i)); System.arraycopy(ab, 0, m_pngBytes, j, i); return j + i; } /**将一个int值写入两个字节里头**/ private int writeInt2(int i, int j) { byte ab[] = { (byte)(i >> 8 & 255), (byte)(i & 255) }; return writeBytes(ab, j); } /**将一个int值写入4个字节里头**/ private int writeInt4(int i, int j) { byte ab[] = { (byte)(i >> 24 & 255), (byte)(i >> 16 & 255), (byte)(i >> 8 & 255), (byte)(i & 255) }; return writeBytes(ab, j); } /**将一个int值i写入pngBytes里头,写的位置为j**/ private int writeByte(int i, int j) { byte ab[] = { (byte)i }; return writeBytes(ab, j); } /**写一个字串**/ private int writeString(String string, int i) { return writeBytes(string.getBytes(), i); } /**写png文件的头**/ private void writeHeader() { int i = m_nBytePos = writeInt4(13, m_nBytePos); m_nBytePos = writeString("IHDR", m_nBytePos); m_nWidth = m_image.getWidth(null); m_nHeight = m_image.getHeight(null); m_nBytePos = writeInt4(m_nWidth, m_nBytePos); m_nBytePos = writeInt4(m_nHeight, m_nBytePos); m_nBytePos = writeByte(8, m_nBytePos); m_nBytePos = writeByte(m_bEncodeAlpha ? 6 : 2, m_nBytePos); m_nBytePos = writeByte(0, m_nBytePos); m_nBytePos = writeByte(0, m_nBytePos); m_nBytePos = writeByte(0, m_nBytePos); m_crc.reset(); m_crc.update(m_pngBytes, i, m_nBytePos - i); m_lCrcValue = m_crc.getValue(); m_nBytePos = writeInt4((int)m_lCrcValue, m_nBytePos); } /**写png文件的中间部分**/ private boolean writeImageData() { int i1 = m_nHeight; int j1 = 0; m_nBytesPerPixel = m_bEncodeAlpha ? 4 : 3; Deflater deflater = new Deflater(m_nCompressionLevel); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024); DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater); try { int k1; byte ab1[]; int i2; int j2; PixelGrabber pixelGrabber; int i3; int[] local; boolean flag2; for (; i1 > 0; i1 -= k1) { k1 = Math.min(32767 / (m_nWidth * (m_nBytesPerPixel + 1)), i1); local = new int[m_nWidth * k1]; pixelGrabber = new PixelGrabber(m_image, 0, j1, m_nWidth, k1, local, 0, m_nWidth); try { pixelGrabber.grabPixels(); } catch (Exception e) { return false; } if ((pixelGrabber.getStatus() & 128) != 0) { return false; } ab1 = new byte[m_nWidth * k1* m_nBytesPerPixel + k1]; if (m_nFilter == 1) m_leftBytes = new byte[16]; if (m_nFilter == 2) m_priorRow = new byte[m_nWidth * m_nBytesPerPixel]; i2 = 0; j2 = 1; for (i3 =0; i3 < m_nWidth * k1; i3++) { if (i3 % m_nWidth == 0) { ab1[i2++] = (byte)m_nFilter; j2 = i2; } ab1[i2++] = (byte)(local[i3] >> 16 & 255); ab1[i2++] = (byte)(local[i3] >> 8 & 255); ab1[i2++] = (byte)(local[i3] & 255); if (m_bEncodeAlpha) ab1[i2++] = (byte)(local[i3] >> 24 & 255); if (i3 % m_nWidth == m_nWidth - 1 && m_nFilter != 0) { if(m_nFilter==1) filterSub(ab1, j2, m_nWidth); else if(m_nFilter==2) filterUp(ab1,j2,m_nWidth); } } deflaterOutputStream.write(ab1, 0, i2); j1 += k1; } deflaterOutputStream.close(); byte ab2[] = byteArrayOutputStream.toByteArray(); int k2 = ab2.length; m_crc.reset(); m_nBytePos = writeInt4(k2, m_nBytePos); m_nBytePos = writeString("IDAT", m_nBytePos); m_crc.update("IDAT".getBytes()); m_nBytePos = writeBytes(ab2, k2, m_nBytePos); m_crc.update(ab2, 0, k2); m_lCrcValue = m_crc.getValue(); m_nBytePos = writeInt4((int)m_lCrcValue, m_nBytePos); deflater.finish(); return true; } catch (IOException e) { System.err.println(e.toString()); } boolean flag1 = false; return flag1; } /**写PNG文件的最后部分**/ private void writeEnd() { m_nBytePos = writeInt4(0, m_nBytePos); m_nBytePos = writeString("IEND", m_nBytePos); m_crc.reset(); m_crc.update("IEND".getBytes()); m_lCrcValue = m_crc.getValue(); m_nBytePos = writeInt4((int)m_lCrcValue, m_nBytePos); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -