




免费预览已结束,剩余23页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
需要四个类,直接应用到项目中,不需要做太多改动,除了导包: 然后在第一个类中传入两个参数,第一个参数为要合成的图片路径数组,第二个类为合成的GIF图片将存在的路径。第一个类: public class JpgToGif public void jpgToGif(String pic, String newPic) try AnimatedGifEncoder1 e = new AnimatedGifEncoder1(); e.setRepeat(1); e.start(newPic); for (int i = 0; i pic.length; i+) / 设置播放的延迟时间 e.setDelay(300); Bitmap src = BitmapFactory.decodeFile(pici); e.addFrame(src); / 添加到帧中 e.finish();/ 刷新任何未决的数据,并关闭输出文件 catch (Exception e) e.printStackTrace(); 第二个类:public class LZWEncoder private static final int EOF = -1; private int imgW, imgH; private byte pixAry; private int initCodeSize; private int remaining; private int curPixel; / GIFCOMPR.C - GIF Image compression routines / / Lempel-Ziv compression based on compress. GIF modifications by / David Rowley () / General DEFINEs static final int BITS = 12; static final int HSIZE = 5003; / 80% occupancy / GIF Image compression - modified compress / / Based on: compress.c - File compression ala IEEE Computer, June 1984. / / By Authors: Spencer W. Thomas (decvax!harpo!utah-cs!utah-gr!thomas) / Jim McKie (decvax!mcvax!jim) / Steve Davies (decvax!vax135!petsd!peora!srd) / Ken Turkowski (decvax!decwrl!turtlevax!ken) / James A. Woods (decvax!ihnp4!ames!jaw) / Joe Orost (decvax!vax135!petsd!joe) int n_bits; / number of bits/code int maxbits = BITS; / user settable max # bits/code int maxcode; / maximum code, given n_bits int maxmaxcode = 1 BITS; / should NEVER generate this code int htab = new intHSIZE; int codetab = new intHSIZE; int hsize = HSIZE; / for dynamic table sizing int free_ent = 0; / first unused entry / block compression parameters - after all codes are used up, / and compression rate changes, start over. boolean clear_flg = false; / Algorithm: use open addressing double hashing (no chaining) on the / prefix code / next character combination. We do a variant of Knuths / algorithm D (vol. 3, sec. 6.4) along with G. Knotts relatively-prime / secondary probe. Here, the modular division first probe is gives way / to a faster exclusive-or manipulation. Also do block compression with / an adaptive reset, whereby the code table is cleared when the compression / ratio decreases, but after the table fills. The variable-length output / codes are re-sized at this point, and a special CLEAR code is generated / for the decompressor. Late addition: construct the table according to / file size for noticeable speed improvement on small files. Please direct / questions about this implementation to ames!jaw. int g_init_bits; int ClearCode; int EOFCode; / output / / Output the given code. / Inputs: / code: A n_bits-bit integer. If = -1, then EOF. This assumes / that n_bits = 254) flush_char(outs); / Clear out the hash table / table clear for block compress void cl_block(OutputStream outs) throws IOException cl_hash(hsize); free_ent = ClearCode + 2; clear_flg = true; output(ClearCode, outs); / reset code table void cl_hash(int hsize) for (int i = 0; i hsize; +i) htabi = -1; void compress(int init_bits, OutputStream outs) throws IOException int fcode; int i /* = 0 */; int c; int ent; int disp; int hsize_reg; int hshift; / Set up the globals: g_init_bits - initial number of bits g_init_bits = init_bits; / Set up the necessary values clear_flg = false; n_bits = g_init_bits; maxcode = MAXCODE(n_bits); ClearCode = 1 (init_bits - 1); EOFCode = ClearCode + 1; free_ent = ClearCode + 2; a_count = 0; / clear packet ent = nextPixel(); hshift = 0; for (fcode = hsize; fcode 65536; fcode *= 2) +hshift; hshift = 8 - hshift; / set hash code range bound hsize_reg = hsize; cl_hash(hsize_reg); / clear hash table output(ClearCode, outs); outer_loop: while (c = nextPixel() != EOF) fcode = (c maxbits) + ent; i = (c = 0) / non-empty slot disp = hsize_reg - i; / secondary hash (after G. Knott) if (i = 0) disp = 1; do if (i -= disp) = 0); output(ent, outs); ent = c; if (free_ent hashtable htabi = fcode; else cl_block(outs); / Put out the final code. output(ent, outs); output(EOFCode, outs); / - void encode(OutputStream os) throws IOException os.write(initCodeSize); / write initial code size byte remaining = imgW * imgH; / reset navigation variables curPixel = 0; compress(initCodeSize + 1, os); / compress and write the pixel data os.write(0); / write block terminator / Flush the packet to disk, and reset the accumulator void flush_char(OutputStream outs) throws IOException if (a_count 0) outs.write(a_count); outs.write(accum, 0, a_count); a_count = 0; final int MAXCODE(int n_bits) return (1 0) cur_accum |= (code = 8) char_out(byte) (cur_accum & 0xff), outs); cur_accum = 8; cur_bits -= 8; / If the next entry is going to be too big for the code size, / then increase it, if possible. if (free_ent maxcode | clear_flg) if (clear_flg) maxcode = MAXCODE(n_bits = g_init_bits); clear_flg = false; else +n_bits; if (n_bits = maxbits) maxcode = maxmaxcode; else maxcode = MAXCODE(n_bits); if (code = EOFCode) / At EOF, write the rest of the buffer. while (cur_bits 0) char_out(byte) (cur_accum & 0xff), outs); cur_accum = 8; cur_bits -= 8; flush_char(outs); 第三个类:public class AnimatedGifEncoder1 protected boolean closeStream; protected int colorDepth; protected byte colorTab; protected int delay = 0; protected int dispose; protected boolean firstFrame; protected int height; protected Bitmap image; protected byte indexedPixels; protected OutputStream out; protected int palSize; protected byte pixels; protected int repeat = -1; protected int sample; protected boolean sizeSet; protected boolean started; protected int transIndex; protected int transparent = 0; protected boolean usedEntry; protected int width; public AnimatedGifEncoder1() boolean arrayOfBoolean = new boolean256; this.usedEntry = arrayOfBoolean; this.palSize = 7; this.dispose = -1; this.closeStream = false; this.firstFrame = true; this.sizeSet = false; this.sample = 10; public boolean addFrame(Bitmap paramBitmap) boolean ok = true; if (paramBitmap = null | !started) return false; try Log.i(AnimatedGifEncode., AnimatedGifEncode is addFrame = + paramBitmap); if (!sizeSet) int i = paramBitmap.getWidth(); int l = paramBitmap.getHeight(); setSize(i, l); this.image = paramBitmap; getImagePixels(); analyzePixels(); if (firstFrame) writeLSD(); writePalette(); if (repeat = 0) writeNetscapeExt(); writeGraphicCtrlExt(); writeImageDesc(); if (!firstFrame) writePalette(); writePixels(); this.firstFrame = false; catch (IOException localIOException1) ok = false; return ok; protected void analyzePixels() int len = this.pixels.length; int nPix = len / 3; byte arrayOfByte1 = new bytenPix; this.indexedPixels = arrayOfByte1; byte arrayOfByte2 = this.pixels; int k = this.sample; NeuQuant nq = new NeuQuant(arrayOfByte2, len, k); this.colorTab = cess(); int l = 0; int i1 = this.colorTab.length; Object localObject; if (l = i1) l = 0; localObject = null; for (int i = 0; i colorTab.length; i += 3) byte temp = colorTabi; colorTabi = colorTabi + 2; colorTabi + 2 = temp; usedEntryi / 3 = false; int k1 = 0; for (int i = 0; i nPix; i+) int index = nq.map(pixelsk1+ & 0xff, pixelsk1+ & 0xff, pixelsk1+ & 0xff); usedEntryindex = true; indexedPixelsi = (byte) index; pixels = null; colorDepth = 8; palSize = 7; if (transparent != 0) transIndex = findClosest(transparent); protected int findClosest(int paramInt) if (colorTab = null) return -1; int r = Color.red(paramInt); int g = Color.green(paramInt); int b = Color.blue(paramInt); int minpos = 0; int dmin = 256 * 256 * 256; int len = colorTab.length; for (int i = 0; i len;) int dr = r - (colorTabi+ & 0xff); int dg = g - (colorTabi+ & 0xff); int db = b - (colorTabi & 0xff); int d = dr * dr + dg * dg + db * db; int index = i / 3; if (usedEntryindex & (d = arrayOfInt.length) return; pixelslocalObject * 3 = (byte) Color .blue(arrayOfIntlocalObject); pixelslocalObject * 3 + 1 = (byte) Color .green(arrayOfIntlocalObject); pixelslocalObject * 3 + 2 = (byte) Color .red(arrayOfIntlocalObject); +localObject; public void setDelay(int ms) delay = Math.round(ms / 10.0f); public void setDispose(int code) if (code = 0) dispose = code; public void setFrameRate(float fps) if (fps != 0f) delay = Math.round(100f / fps); public void setQuality(int quality) if (quality = 0) Log.i(AnimatedGifEncode., AnimatedGifEncode is setRepeat.setRepeat =); repeat = iter; public void setSize(int w, int h) if (started & !firstFrame) return; width = w; height = h; if (width 1) width = 320; if (height = 0) disp = dispose & 7; / user override disp = 2; / packed fields out.write(0 | / 1:3 reserved disp | / 4:6 disposal 0 | / 7 user input - 0 = none transp); / 8 transparency flag writeShort(delay); / delay x 1/100 sec out.write(transIndex); / transparent color index out.write(0); / block terminator protected void writeImageDesc() throws IOException out.write(0x2c); / image separator writeShort(0); / image position x,y = 0,0 writeShort(0); writeShort(width); / image size writeShort(height); / packed fields if (firstFrame) / no LCT - GCT is used for first
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 创设真实情境提升小学英语学习兴趣的策略
- 跨学科合作促进农业专业数字素养的培养路径
- 多元化新媒体渠道助力大学生就业创业心理疏导
- 中国石油青海销售分公司高校毕业生招聘考试真题2024
- 财务风险预警机制的建设与运行效果分析
- 儿童英语培训课件
- 拍卖概论模拟试题及答案
- 汤姆索亚历险记知识竞赛题及答案
- 2025年说文解字考试题目及答案
- 2025年慈溪国企考试题目及答案
- 2025年多媒体与网络技术专业考试试题及答案
- 高中英语译林版必修第一册课文语法填空知识点
- DZ/T 0254-2014页岩气资源/储量计算与评价技术规范
- 注塑行业仓库管理制度
- DB32/T 3691-2019 成品住房装修技术标准
- 2025年广东惠州市交通投资集团有限公司招聘笔试参考题库附带答案详解
- 能源系统工程课件
- 学校膳食监督家长委员会章程
- 《新能源汽车发展历程》课件
- 学校承接社会考试协议书
- 护罩钣金合同协议
评论
0/150
提交评论