Skip to content

Instantly share code, notes, and snippets.

@icarofreire
Created June 25, 2016 06:47
Show Gist options
  • Save icarofreire/97d549d62c02a7dd65d5abc81d4dc625 to your computer and use it in GitHub Desktop.
Save icarofreire/97d549d62c02a7dd65d5abc81d4dc625 to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package joimages;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Vector;
import javax.imageio.ImageIO;
/**
*
* @author icaro
*/
public class Joimages {
private static int n = 100;
private static final int COMBINE_IMG_WIDTH = 180;
private static final int COMBINE_IMG_HEIGHT = 90;
private static String nome_pasta_salvar = "full-images-scenes";
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
String diretorio = "/DIR/DIR/DIR/DIR";
Vector<String> p = todas_as_pastas(diretorio);
for (int i = 0; i < p.size(); i++) {
String get = p.get(i);
System.out.println( get );
}
// criar_thumbnail(diretorio);
}
public static void criar_pasta()
{
File file = new File(nome_pasta_salvar);
if (!file.exists()) {
if (file.mkdir()) {
// System.out.println("Directory is created!");
} else {
// System.out.println("Failed to create directory!");
}
}
}
public static Vector<String> todas_as_pastas(final String diretorio)
{
Vector<String> pastas = new Vector<String>();
File folder = new File(diretorio);
for (final File fileEntry : folder.listFiles()) {
if (fileEntry.isDirectory()) {// <= procura recursivamente;
String pasta = fileEntry.getAbsolutePath();
pastas.add( pasta );
}
}
return pastas;
}
public static void criar_thumbnail(final String diretorio) throws IOException
{
criar_pasta();
int n = 10;
int rows = n; //we assume the no. of rows and cols are known and each chunk has equal width and height
int cols = n;
int chunks = rows * cols;
int chunkWidth, chunkHeight;
int type;
Vector<String> ll = listar_imagens(diretorio);
//creating a bufferd image array from image files
BufferedImage[] buffImages = new BufferedImage[chunks];
for (int i = 0; i < ll.size(); i++) {
buffImages[i] = resize_image(ImageIO.read(new File(ll.elementAt(i))));
}
type = buffImages[0].getType();
chunkWidth = buffImages[0].getWidth();
chunkHeight = buffImages[0].getHeight();
//Initializing the final image
BufferedImage finalImg = new BufferedImage(chunkWidth*cols, chunkHeight*rows, type);
int num = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
finalImg.createGraphics().drawImage(buffImages[num], chunkWidth * j, chunkHeight * i, null);
num++;
}
}
// new exibir_imagem( finalImg );
ImageIO.write(finalImg, "jpeg", new File( nome_pasta_salvar + "/FULL--" + gerar_chave(10) + ".jpg"));
System.out.println("["+ diretorio + "] - Ok.");
}
public static Vector<String> listar_imagens(final String diretorio)
{
Vector<String> imagens = new Vector<String>();
File folder = new File(diretorio);
for (final File fileEntry : folder.listFiles())
{
if (fileEntry.isDirectory()) {// <= procura recursivamente;
listar_imagens(fileEntry.getAbsolutePath());
} else {
String arquivo = fileEntry.getName();
if(
(arquivo.indexOf(".jpg") != -1) ||
(arquivo.indexOf(".JPG") != -1) ||
(arquivo.indexOf(".jpeg") != -1) ||
(arquivo.indexOf(".JPEG") != -1) ||
(arquivo.indexOf(".png") != -1) ||
(arquivo.indexOf(".PNG") != -1)
)
{
// System.out.println(fileEntry.getAbsolutePath());
imagens.add(fileEntry.getAbsolutePath());
}
}
}
return imagens;
}
public static BufferedImage resize_image(BufferedImage originalImage){
BufferedImage resizedImage = new BufferedImage(COMBINE_IMG_WIDTH+n, COMBINE_IMG_HEIGHT+n, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, COMBINE_IMG_WIDTH+n, COMBINE_IMG_HEIGHT+n, null);
g.dispose();
return resizedImage;
}
public static String gerar_chave(int tamanho)
{
String alphabet = new String("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
int n = alphabet.length();
String result = new String();
Random r = new Random();
for (int i=0; i<tamanho; i++){
result = result + alphabet.charAt(r.nextInt(n));
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment