




已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
将Image对像转换为png格式的byte数组 作者:lupenglo文章来源:本站原创点击数:4629更新时间:2008-5-18 由于J2me中没有ImageIO 导致一些功能无法在J2me上实现.导致大家无法实现一些功能.如绘图软件.大头贴.截屏等.貌私新版的jsr什么什么实现了将Image 输出为png or jpg但估计等到这种手机上市的时候以是几年后的事情了.其实在j2me上将Image 对象转成png格式的数组并不是不可能的任务.原理上说1 获取Image 的ARGB数据2将ARGB转换成PNG存储用的的RGBA格式3RGBA格式的数据还要做些小的处理每行后面加一个byte 04用LZ77方法将RGBA格式的数组压缩5附加正确格式PNG24文件头输出即可包括IHEAD IHDR IEND IDAT外壳但是使用LZ77压缩手机上无论时间或空间都是不太能接受的还好LZ77 存在一钟无压缩的压缩方法本文的方法就是使用无压缩的方式搞定数据压缩的参阅了jzlib 代码使用300行左右的代码搞定了将Image对象转pngimport javax.microedition.lcdui.*;import java.io.*;import javax.microedition.io.file.FileConnection;import javax.microedition.io.Connector;public class CGame extends Canvas /Image2Bytes by AnderLu /生成的byte数组可直接用于外部存储为.png格式的图片文件看图软件可直接打开 public static int IDATPOS; public static byte HEADChunk = (byte) 0x89, (byte) 0x50, (byte) 0x4E, (byte) 0x47, (byte) 0x0D, (byte) 0x0A, (byte) 0x1A, (byte) 0x0A, ; public static byte tRNSChunk = (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x74, (byte) 0x52, (byte) 0x4E, (byte) 0x53, (byte) 0x00, (byte) 0x40, (byte) 0xE6, (byte) 0xD8, (byte) 0x66, ; public static byte IENDChunk = /PNGIEND (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x49, (byte) 0x45, (byte) 0x4E, (byte) 0x44, (byte) 0xAE, (byte) 0x42, (byte) 0x60, (byte) 0x82 ; Image img; public CGame() Image img = null; try img = Image.createImage(/cap.png); catch (IOException ex) ex.printStackTrace(); byte data = Image2Bytes(img); this.img = Image.createImage(data, 0, data.length); saveFile(file:/e:/a.png, data); protected void paint(Graphics g) g.setColor(0xffffff); g.fillRect(0, 0, 240, 320); g.drawImage(img, 0, 0, 0); /*保存文件 * path:路径 * fileData:文件数据 * return: 0:出现异常,1:保存成功 */ public int saveFile(String path, byte fileData) FileConnection fc = null; try fc = (FileConnection) Connector.open(path, Connector.READ_WRITE); if (!fc.exists() fc.create(); OutputStream os = fc.openOutputStream(); os.write(fileData); os.flush(); os.close(); fc.close(); return 1; catch (IOException ex) ex.printStackTrace(); return 0; public byte Image2Bytes(Image img) try int w = img.getWidth(); int h = img.getHeight(); int offset = 0; byte buffer = new byte(w * 4 + 1) * h + offset; getImageBufferForImageARGB8888(img, buffer, w, h, offset); System.gc(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(baos); WritePng(dout, w, h, buffer, null, false, offset); byte data = baos.toByteArray(); writeCRC(data, 8); /更新IHDR CRC writeCRC(data, 33); /更新PLTE CRC writeCRC(data, IDATPOS); /更新IDAT CRC buffer = null; System.gc(); return data; catch (IOException ex) ex.printStackTrace(); return null; public static void writeCRC(byte data, int chunkpos) int chunklen = (datachunkpos & 0xFF) 24) | (datachunkpos + 1 & 0xFF) 16) | (datachunkpos + 2 & 0xFF) 24); datapos + 1 = (byte) (val & 0xFF0000) 16); datapos + 2 = (byte) (val & 0xFF00) 8); datapos + 3 = (byte) (val & 0xFF); public static int crc_table; /CRC 表 public static int CRCChecksum(byte buf, int off, int len) int c = 0xffffffff; int n; if (crc_table = null) int mkc; int mkn, mkk; crc_table = new int256; for (mkn = 0; mkn 256; mkn+) mkc = mkn; for (mkk = 0; mkk 1); else mkc = mkc 1; crc_tablemkn = mkc; for (n = off; n 8); return c; public static long adler32(long adler, byte buf, int index, int len) int BASE = 65521; int NMAX = 5552; /TODO remove this function at all if (buf = null) return 1L; long s1 = adler & 0xffff; long s2 = (adler 16) & 0xffff; int k; while (len 0) k = len = 16) s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; s1 += bufindex+ & 0xff; s2 += s1; k -= 16; if (k != 0) do s1 += bufindex+ & 0xff; s2 += s1; while (-k != 0); s1 %= BASE; s2 %= BASE; return (s2 16) | s1; public static void WritePng(DataOutputStream output, int width, int height, byte buffer, byte colors, boolean Transparent, int offset) throws IOException int adler = (int) adler32(1l, buffer, offset, buffer.length - offset); byte lenNlen = /压缩块的LEN和NLEN信息 (byte) 0, (byte) 0xfa, (byte) 0x7e, (byte) 0x05, (byte) 0x81 ; IDATPOS = 0; output.write(HEADChunk); IDATPOS += HEADChunk.length; /写IHDR output.writeInt(13); /len output.writeInt(1229472850); /IHDR type code output.writeInt(width); /写宽度 output.writeInt(height); /写高度 output.writeByte(8); /1Bitdepth if (colors = null) output.writeByte(6); /2ColorType else output.writeByte(3); /2ColorType output.writeByte(0); /3CompressionMethod output.writeByte(0); /4Filter method output.writeByte(0); /5Interlace method output.writeInt(0); /写crc IDATPOS += 25; /写PLTE if (colors != null) output.writeInt(colors.length); /len output.writeInt(1347179589); /type code output.write(colors); /data output.writeInt(0); /crc IDATPOS += colors.length + 12; /写TRNS if (Transparent) output.write(tRNSChunk); IDATPOS += tRNSChunk.length; /写IDAT byte dpixels = buffer; int bufferlen = dpixels.length - offset; int blocklen = 32506; int blocknum = 1; if (dpixels.length % blocklen) = 0) blocknum = bufferlen / blocklen; else blocknum = (bufferlen / blocklen) + 1; int IDATChunkLen = (bufferlen + 6 + blocknum * 5); output.writeInt(IDATChunkLen); /len output.writeInt(1229209940); /idat type code output.writeShort(short) 0x78da); /78da for (int i = 0; i = blocklen) len = blocklen; lenNlen0 = (byte) 0; else lenNlen0 = (byte) 1; int msb = (len & 0xff); int lsb = (len 8); lenNlen1 = (byte) msb; lenNlen2 = (byte) lsb; lenNlen3 = (byte) (msb 0xff); lenNlen4 = (byte) (lsb 0xff); output.write(lenNlen); output.write(dpixels, off + offset, len); output.writeIn
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 江苏省常州市2025年中考物理真题附真题答案
- 安全教育培训闭幕词课件
- 蔻驰ai面试题库大全及答案
- 安全教育培训课程开发课件
- 安全教育培训课时安排课件
- 学前教育机构师资队伍教育政策对师资队伍影响研究报告
- 海洋能发电技术创新驱动2025年海岛能源供应体系升级报告
- 农业与食品行业绿色生产技术发展研究报告
- 银行零售业务数字化营销转型中的金融营销策略创新实践报告
- 新能源行业安全生产标准化建设与新能源电池安全2025年报告
- 混凝土模板工程验收表(含续表)GDAQ2090202
- GB/T 29466-2023板式热交换器机组
- 多模态大模型技术演进及研究框架
- 中国教育史全套
- GB/T 818-2000十字槽盘头螺钉
- GB/T 31298-2014TC4钛合金厚板
- 口腔科中医临床诊疗技术
- itop-4412开发板之精英版使用手册
- 老年肌肉衰减综合征肌少症培训课件
- 中学生物学教学技能与实践课件
- 井喷失控事故案例教育-井筒工程处课件
评论
0/150
提交评论