Last active
March 29, 2018 13:38
-
-
Save dogterbox/4cc01c5c1cb08f5327e8273e1cf3a216 to your computer and use it in GitHub Desktop.
Image to GIF file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package CG; | |
import java.awt.Color; | |
import java.awt.Graphics; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.Scanner; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
public class MainWork2 extends JPanel { | |
private static MyImage[] imgFile = null; | |
private static ArrayList<int[]> arrImg = new ArrayList<int[]>(); | |
private static long pauseTime; | |
public static void main(String[] args) throws IOException { | |
try { | |
ArrayList<File> listOfImg = new ArrayList<File>(); | |
//input pause time for animate GIF | |
while(true) { | |
try { | |
System.out.print("Enter pause time for animate GIF (millisecond) : "); | |
pauseTime = new Scanner(System.in).nextLong(); | |
if(pauseTime >= 0) break; | |
else System.err.println("Input value >= 0"); | |
}catch(Exception e) { | |
System.err.println("Input integer only [ >= 0 ]"); | |
} | |
} | |
//input image file | |
while(true){ | |
System.out.print("Enter absolute path of image folder: "); | |
String folderPath = new Scanner(System.in).nextLine(); | |
File folder = new File(folderPath); | |
if(folder.isDirectory()) { | |
File[] listOfFiles = folder.listFiles(); | |
for (int i = 0; i < listOfFiles.length; i++) { | |
File file = listOfFiles[i]; | |
if (file.isFile() && MyImage.isImage(file)) { //is file && is image | |
listOfImg.add(file); | |
System.out.println("File: " + file.getAbsoluteFile()); | |
} | |
}//for list of files | |
break; | |
}else { | |
System.err.print("It's NOT a folder\n\n"); | |
} | |
} | |
int numFile = listOfImg.size(); | |
imgFile = new MyImage[numFile]; | |
for(int i=0; i<numFile; i++) { | |
imgFile[i] = new MyImage(listOfImg.get(i)); | |
//convert image to array | |
//add array to arrImg | |
arrImg.add(imgFile[i].toArray()); | |
} | |
//new and set JFrame | |
JFrame f = new JFrame(); | |
f.add(new MainWork2()); | |
f.setVisible(true); | |
f.setResizable(false); | |
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
//set size //based on the size of the first image. | |
f.setSize(imgFile[0].getWidth(),imgFile[0].getHeight()); | |
f.repaint(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private int state = 0; | |
@Override | |
public void paint(Graphics g){ | |
try { | |
if(arrImg != null) { | |
System.out.println("State show: "+state); | |
int width = imgFile[state].getWidth(); | |
int height = imgFile[state].getHeight(); | |
int[] img = arrImg.get(state++); | |
//draw image | |
for(int index=0,y=0; y<height; y++) { | |
for(int x=0; x<width; x++) { | |
plot(g,x,y,img[index++]); | |
} | |
} | |
Thread.sleep(pauseTime); //sleep //pause animate GIF | |
if(state >= arrImg.size()) state=0; | |
repaint(); | |
}//if arr is null | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
private void plot(Graphics g, int x, int y, int intColor) { | |
g.setColor(new Color(intColor)); | |
g.drawLine(x, y, x, y); //draw pixel | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package CG; | |
import java.awt.Image; | |
import java.awt.image.PixelGrabber; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import javax.activation.MimetypesFileTypeMap; | |
import javax.imageio.ImageIO; | |
public class MyImage { | |
private File imgFile = null; | |
private Image img = null; | |
public MyImage(File imgFile) throws IOException{ | |
this.imgFile = imgFile; | |
img = ImageIO.read(imgFile); | |
} | |
public static boolean isImage(File f) { | |
String mimetype= new MimetypesFileTypeMap().getContentType(f); | |
String type = mimetype.split("/")[0]; | |
return type.equals("image"); | |
} | |
public int getWidth() { | |
return img.getWidth(null); | |
} | |
public int getHeight() { | |
return img.getHeight(null); | |
} | |
public int[] toArray(){ | |
int[] pixel = null; | |
try { | |
PixelGrabber grabber = new PixelGrabber(img, 0, 0, getWidth(), getHeight(), true); | |
if (grabber.grabPixels()) { | |
pixel = (int[]) grabber.getPixels(); | |
} | |
writeLog(pixel); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return pixel; | |
} | |
private void writeLog(int[] arr) { | |
/* | |
* Log file index of first[0] and second[1] is image size -- first[0] value is image width and | |
* second[1] value is image height, other value is color integer value | |
* Tokens by " " (blank). There are values totally = width*height. | |
*/ | |
try{ | |
String filenameSave = imgFile.getAbsolutePath()+".txt"; | |
PrintWriter writer = new PrintWriter(filenameSave, "UTF-8"); | |
int pixelLength = arr.length; | |
//writer | |
writer.print(getWidth()); //first[0] write image width | |
writer.print(" "+getHeight()); //second[1] write image height | |
for(int i=0; i<pixelLength; i++) { | |
writer.print(" "+arr[i]); | |
} | |
writer.close(); | |
System.out.println("Save file: "+filenameSave); | |
} catch (IOException e) { | |
System.err.println(e.getMessage()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On image folder -- include all images in the same folder
Result image -- output
Source image file
https://78.media.tumblr.com/f15ea99b3a02831363c06726d2640fed/tumblr_mrw2kmWOJF1qhl91yo1_500.gif