Created
June 11, 2018 11:06
-
-
Save MaxSWR/3b04eb460296fe3db2cce5c2cf6e2d9e to your computer and use it in GitHub Desktop.
Main class
This file contains 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 a4toa5; | |
import java.awt.Graphics2D; | |
import java.awt.geom.AffineTransform; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.imageio.ImageIO; | |
public class A4toA5 { | |
private static BufferedImage templateIMG; | |
private static int subWidth; | |
private static int subHeight; | |
private static File folderIN; | |
private static File folderOUT; | |
private static File template; | |
private static File[] images; | |
private static int count = 0; | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) throws IOException { | |
folderIN = new File(...); | |
folderOUT = new File(...); | |
template = new File(...); | |
templateIMG = ImageIO.read(template); | |
subWidth = templateIMG.getWidth(); | |
subHeight= templateIMG.getHeight()/2; | |
images = folderIN.listFiles(); | |
int countImages = images.length; | |
for (int i = 0; i < countImages ; i = i + 4) { | |
make2A4(i); | |
} | |
} | |
/** | |
* Сдеалть два листа А4 | |
* @param i - номер четверки листа А5 | |
*/ | |
public static void make2A4(int i) { | |
try { | |
BufferedImage template1 = ImageIO.read(template); | |
BufferedImage template2 = ImageIO.read(template); | |
File a5_1 = i < images.length ? images[i] : null; | |
File a5_2 = i + 1 < images.length ? images[i + 1] : null; | |
File a5_3 = i + 2 < images.length ? images[i + 2] : null; | |
File a5_4 = i + 3 < images.length ? images[i + 3] : null; | |
addSubImages(template1, new File[] {a5_4, a5_1}); | |
System.out.println("Curent: " + images[i+1].getAbsolutePath()); | |
addSubImages(template2, new File[] {a5_2, a5_3}); | |
System.out.println("Curent: " + images[i+3].getAbsolutePath()); | |
writeImage(template1, count++); | |
writeImage(template2, count++); | |
} catch (IOException ex) { | |
Logger.getLogger(VoenkaKonspekt.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
/** | |
* Добавить изображение подлиста | |
* @param subImg - изображение подлиста | |
* @param side - верх или низ | |
*/ | |
public static void addSubImages(BufferedImage src, File[] subImg) { | |
try { | |
BufferedImage[] img = new BufferedImage[2]; | |
for (int i = 0; i < 2; i++) { | |
img[i] = scale(ImageIO.read(subImg[i]), subHeight, subWidth); | |
img[i] = rotate90(img[i]); | |
} | |
src.getGraphics().drawImage(img[0], 0, 0, null); | |
src.getGraphics().drawImage(img[1], 0, subHeight, null); | |
} catch (IOException ex) { | |
Logger.getLogger(VoenkaKonspekt.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
/** | |
* Запись изображения в файл | |
* @param count - номер изображения | |
*/ | |
public static void writeImage(BufferedImage src, int count) { | |
try { | |
String folder; | |
if(count % 2 == 0) { | |
folder = "\\1\\"+count+".png"; | |
} else { | |
folder = "\\2\\"+count+".png"; | |
} | |
File out = new File(folderOUT.getAbsolutePath() + folder); | |
ImageIO.write(src, "png", out); | |
src.flush(); | |
} catch (IOException ex) { | |
Logger.getLogger(VoenkaKonspekt.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
/** | |
* Повернуть изображение на 90 градусов | |
* @param src - исходное изображение | |
* @return перевернутое изображение | |
*/ | |
public static BufferedImage rotate90(BufferedImage src) { | |
if (src == null) return null; | |
BufferedImage out = new BufferedImage(src.getHeight(), src.getWidth(), src.getType()); | |
Graphics2D gRotated = out.createGraphics(); | |
AffineTransform at = new AffineTransform(); | |
at.translate((double)out.getWidth()/2, (double)out.getHeight()/2); | |
at.rotate(Math.toRadians(90)); | |
at.translate(-(double)out.getHeight()*0.5, -(double)out.getWidth()*0.5); | |
gRotated.setTransform(at); | |
gRotated.drawImage(src, 0, 0, null); | |
return out; | |
} | |
/** | |
* Масштабировать изображение | |
* @param src - исходное изображение | |
* @param newW - новая ширина | |
* @param newH - новая высота | |
* @return - отмасштабированное изображение | |
*/ | |
public static BufferedImage scale(BufferedImage src, int newW, int newH) { | |
if (src == null) return null; | |
BufferedImage out = new BufferedImage(newW, newH, src.getType()); | |
out.getGraphics().drawImage(src, 0, 0, newW, newH, null); | |
return out; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment