關(guān)于我儒将,歡迎關(guān)注
csdn博客:ccapton(http://blog.csdn.net/ccapton) 微信:Ccapton
Github: (https://www.github.com/ccapton)
個人博客:http://www.ccapton.cn
Gif編碼器 gifencoder
gifencoder 這是一個Java寫的圖片轉(zhuǎn)Gif的框架 https://github.com/square/gifencoder
![](https://raw.githubusercontent.com/Ccapton/FileRepertory/master/files/apple.gif)
本來想搜一下視頻轉(zhuǎn)Gif的相關(guān)框架的,找到了一個用JavaScript寫 https://github.com/vvo/gifify ,但是我在安裝其配置需求 ImageMagick 時發(fā)現(xiàn) 用命令$ brew install imagemagick --with-fontconfig時喇聊,安裝到最后總是卡住了戒洼,去掉--with-fontconfig安裝倒是能裝進去,可是最后代碼運行時又提醒我需要設(shè)置ImageMagick睡榆,弄來弄去還是沒成功-_-!场航。于是便找到了圖片轉(zhuǎn)Gif的gifencoder這個框架
gifencoder用法
這是gifencoder的Github頁上的基礎(chǔ)用法
有關(guān)問題
但是沒有交代清楚存放圖片rgb顏色數(shù)據(jù)的二維整形數(shù)組該怎么獲取缠导,在Android上,好像并沒有直接獲得二維數(shù)據(jù)的方法旗闽。于是我嘗試了用Bitmap的copyPixelsToBuffer()方法獲取一維數(shù)組酬核,如下所示
public int[] convertImageToArray(File file) {
Bitmap bitmap= BitmapFactory.decodeFile(file.getAbsolutePath());
int rgbArray[]=new int[bitmap.getAllocationByteCount()];
Buffer buffer=IntBuffer.wrap(rgbArray);
bitmap.copyPixelsToBuffer(buffer);
bitmap.recycle();
return rgbArray;
}
然后調(diào)用GifEncoder的另一個addImage方法
/**
* Add an image to the GIF file.
*
* @param rgbData an image buffer in RGB format
* @param width the number of pixels per row in the pixel array
* @param options options to be applied to this image
* @return this instance for chaining
* @throws IOException if there was a problem writing to the given output stream
*/
public GifEncoder addImage(int[] rgbData, int width, ImageOptions options) throws IOException {
addImage(Image.fromRgb(rgbData, width), options);
return this;
}
可是當(dāng)我跑起來時,不知道是不是因為參數(shù)不正確還是咋地,怎么修改都會出現(xiàn)手機內(nèi)存吃緊"Starting a blocking GC Alloc",最后強制退出的問題他宛,
輸出的gif圖片也是只有1kb,即轉(zhuǎn)換失敗了蔬螟,我覺得一開始獲取一維數(shù)組的時候出現(xiàn)問題了,導(dǎo)致后面數(shù)據(jù)處理時間太長了汽畴。后來我還是放棄了gitencoder在Android上的應(yīng)用了,果斷選擇在
桌面端Java平臺上繼續(xù)試旧巾。
桌面端Java使用gitencoder
我使用的是IntelliJ IDEA,直接下載jar包,引入java工程內(nèi),我使用這個框架的代碼就下面這么點忍些,就能輸出想要的gif圖片
import com.squareup.gifencoder.GifEncoder;
import com.squareup.gifencoder.ImageOptions;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
public class Main {
public static void main(String[] args) {
// 讀取圖片到BufferedImage
BufferedImage bf = readImage("/Users/capton/Desktop/apple1.png");//這里寫你要讀取的絕對路徑+文件名
BufferedImage bf2 = readImage("/Users/capton/Desktop/apple2.png");//這里寫你要讀取的絕對路徑+文件名
BufferedImage bf3 = readImage("/Users/capton/Desktop/apple3.png");//這里寫你要讀取的絕對路徑+文件名
BufferedImage bf4 = readImage("/Users/capton/Desktop/apple4.png");//這里寫你要讀取的絕對路徑+文件名
BufferedImage bf5 = readImage("/Users/capton/Desktop/apple5.png");//這里寫你要讀取的絕對路徑+文件名
// 將圖片轉(zhuǎn)換為二維數(shù)組
int[][] rgbArray1 = convertImageToArray(bf);
int[][] rgbArray2= convertImageToArray(bf2);
int[][] rgbArray3= convertImageToArray(bf3);
int[][] rgbArray4= convertImageToArray(bf4);
int[][] rgbArray5= convertImageToArray(bf5);
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream("/Users/capton/Desktop/apple.gif");
ImageOptions options = new ImageOptions();
GifEncoder encoder=new GifEncoder(outputStream, 161, 182, 0);
encoder.addImage(rgbArray1, options);
encoder.addImage(rgbArray2, options);
encoder.addImage(rgbArray3, options);
encoder.addImage(rgbArray4, options);
encoder.addImage(rgbArray5, options);
encoder.finishEncoding();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
} finally{
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static BufferedImage readImage(String imageFile){
File file = new File(imageFile);
BufferedImage bf = null;
try {
bf = ImageIO.read(file);
} catch (IOException e) {
e.printStackTrace();
}
return bf;
}
public static int[][] convertImageToArray(BufferedImage bf) {
// 獲取圖片寬度和高度
int width = bf.getWidth();
int height = bf.getHeight();
// 將圖片sRGB數(shù)據(jù)寫入一維數(shù)組
int[] data = new int[width*height];
bf.getRGB(0, 0, width, height, data, 0, width);
// 將一維數(shù)組轉(zhuǎn)換為為二維數(shù)組
int[][] rgbArray = new int[height][width];
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
rgbArray[i][j] = data[i*width + j];
return rgbArray;
}
}
其中' GifEncoder encoder=new GifEncoder(outputStream, 161, 182, 0);'這里161鲁猩,182是圖片的寬高(px),我們要把每一張圖片的寬高都統(tǒng)一,然后把寬高通過這句代碼設(shè)置完畢
5張不同方向黑白漸變的蘋果logo罢坝,把它們整合成gif后廓握,就出現(xiàn)下面的效果了
![](https://raw.githubusercontent.com/Ccapton/FileRepertory/master/files/apple.gif)
后記
這里使用了BufferImage這個類,它是基于java的AWT控件的嘁酿,而Android上并不支持AWT隙券,于是也就不能用BufferImage了
,之前我的方法又不對闹司。所以請問大家娱仔,有誰知道怎么在Android上使用這個gifencoder框架的嗎?
在這之后游桩,我打算仔細(xì)研究一些這個框架牲迫,用AWT把它做出圖像界面來,這樣方便大家使用這樣一個java版的圖片轉(zhuǎn)Gif軟件可好借卧?