Package boneyiyi. tool. image;
Import java. AWT. color;
Import java. AWT. graphics2d;
Import java. AWT. image. bufferedimage;
Import java. Io. file;
Import java. Io. ioexception;
Import java. util. arraylist;
Import java. util. Random;
Import javax. ImageIO. ImageIO;
/**
* In order to save time for Yiyi, I wrote a robot that can automatically splice images with watermarks. Many bugs can be used.
*
* Lazybone 2012.04.05
*/
Public class utils {
**********************/
// Whether to save only the final work to the work folder
Static Boolean issavefinalworks = true;
// The height of filling up and down each image,
Static final int modify = 50;
// Whether to add a watermark
Static Boolean isaddlogo = true;
// Number of watermark searches
Static final int logomodify = 80;
/************* No configuration is required *********************/
// Studio folder "D:/boneyiyi"
Static final string workspace = "D:/boneyiyi ";
// Watermark folder
Static final string logoapng = workspace + "/material/logo_425_198.png ";
Static final string logoapngtest = workspace + "/material/logoneappspng ";
// Studio folder "D:/boneyiyi"
Static final string workspace_todo = workspace + "/Workshop ";
// Filename suffix for search
Static final string filename_750wok = "_750wok.jpg ";
// Picture on top of the baby's inner page
Static final string titlefilename = workspace + "/material/title.jpg ";
// Filename suffix for search
Static final string filename_ OK = "_ OK .jpg ";
// File folder
Static final string workspace_done = workspace + "/complete work ";
/**
* Start
*
* @ Param ARGs
*/
Public static void main (string ARGs []) {
New utils (). startwork ();
}
Public void startwork (){
// Initialize the logo
Initlogo ();
File [] files = new file (workspace_todo). listfiles ();
For (INT I = 0; I <files. length; I ++ ){
If (files [I]. isdirectory ()){
System. Out. println (files [I]. getpath () + "is doing ...");
New utils (). createthepic (files [I]);
System. Out. println (files [I]. getpath () + "is done .");
System. Out. println ();
}
}
System. Out. println ("well done! ");
}
Public void createthepic (File mainfile ){
Graphics2d panel;
Graphics2d panel750;
Int countlen = 0;
Int width = 0;
Int curx = 0;
Int Cury = 0;
Arraylist <bufferedimage> imagelist = new arraylist <bufferedimage> ();
File [] files = mainfile. listfiles ();
For (INT I = 0; I <files. length; I ++ ){
If (files [I]. getpath (). Contains (filename_750wok)
| Files [I]. getpath (). Contains (filename_ OK )){
Continue;
}
Bufferedimage bimage = loadimagelocal (files [I]. getpath ());
Countlen + = bimage. getheight ();
Imagelist. Add (bimage );
Width = (width> bimage. getwidth ()? Width: bimage. getwidth ());
}
// If the image is small and smaller than 750, the default width is 750;
Width = (width & gt; 750? Width: 750 );
Bufferedimage panelimage = new bufferedimage (width, countlen,
Bufferedimage. type_int_rgb );
Panel = panelimage. creategraphics ();
Panel. setbackground (color. White );
Panel. setcolor (color. White );
Panel. fillrect (0, 0, width, countlen );
// Add a title
Bufferedimage Title = loadimagelocal (titlefilename );
Curx = (width-title. getwidth ()/2;
Panel. drawimage (title, curx, Cury, title. getwidth (), title. getheight (),
Null );
Cury + = title. getheight ();
For (INT I = 0; I <imagelist. Size (); I ++ ){
Panel. setcolor (color. White );
Bufferedimage IMG = imagelist. Get (I );
Curx = (width-IMG. getwidth ()/2;
// Remove the virtual edge
Panel. drawimage (IMG, curx, Cury, IMG. getwidth (), IMG. getheight (),
Null );
Panel. fillrect (curx, Cury, IMG. getwidth (), modify );
Panel. fillrect (curx, Cury + IMG. getheight ()-Modify,
IMG. getwidth (), IMG. getheight ());
// Add a watermark
If (isaddlogo = true ){
Drawlogo (panel, (width-750)/2, Cury + modify, 750,
IMG. getheight ()-Modify );
}
Cury + = IMG. getheight ();
}
// Folder name, used to differentiate files in different folders
String fnfirst = mainfile. getpath (). Replace (mainfile. getparent (),"");
If (issavefinalworks = false ){
String fnall = mainfile. getpath () + "/" + fnfirst + "_" + width
+ "_" + Countlen + filename_ OK; // "_ OK .jpg"
Writeimagelocal (fnall, panelimage );
System. Out. println (fnall + "OK ");
}
// Cut 750
Bufferedimage panelimage750 = new bufferedimage (750, countlen,
Bufferedimage. type_int_rgb );
Panel750 = panelimage750.creategraphics ();
Panel750.setbackground (color. White );
Panel750.setcolor (color. White );
Panel750.fillrect (0, 0,750, countlen );
Panel750.drawimage (panelimage, 0, 0,750, countlen, (width-750)/2,
0,750 + (width-750)/2, countlen, null );
// Write image 750
If (issavefinalworks = false ){
String fn750 = mainfile. getpath () + "/" + fnfirst + "_ 750 _"
+ Countlen + filename_750wok; // "_750wok.jpg ";
Writeimagelocal (fn750, panelimage750 );
System. Out. println (fn750 + "OK ");
}
// Write the image to the outer folder
File done = new file (workspace_done );
If (! Done. exists ()){
Done. mkdir ();
}
String fn750outside = workspace_done + "/" + fnfirst + "_ 750 _"
+ Countlen + filename_750wok; // "_750wok.jpg ";
Writeimagelocal (fn750outside, panelimage750 );
// Close
Panel. Dispose ();
Panel750.dispose ();
}
/**
* Import local images to the buffer zone
*/
Public static bufferedimage loadimagelocal (string imgname ){
Try {
Return ImageIO. Read (new file (imgname ));
} Catch (ioexception e ){
System. Out. println (E. getmessage ());
}
Return NULL;
}
/**
* Generate a new image to the local device.
*/
Public static void writeimagelocal (string newimage, bufferedimage IMG ){
If (newimage! = NULL & IMG! = NULL ){
Try {
File outputfile = new file (newimage );
ImageIO. Write (IMG, "jpg", outputfile );
} Catch (ioexception e ){
System. Out. println (E. getmessage ());
}
}
}
/**
* Draw a logo
*/
Public void drawlogo (graphics2d panel, int X, int y, int W, int h ){
Initlogo ();
Int logow = logobufferedimage. getwidth ();
Int logoh = logobufferedimage. getheight ();
// When logomodify draws the logo, draw it in
Int startx = x + logomodify
+ New random (). nextint (W-logow-logomodify );
Int starty = Y + logomodify
+ New random (). nextint (H-logoh-logomodify );
Panel. drawimage (logobufferedimage, startx, starty, logow, logoh, null );
}
/**
* Initialize the logo
*/
Public void initlogo (){
If (logobufferedimage! = NULL ){
Return;
}
Bufferedimage IMG;
Try {
// Read transparent PNG
IMG = ImageIO. Read (new file (logoapng ));
// Generate an array for storing PNG data
Int RGB [] = new int [img. getwidth () * IMG. getheight ()];
// Read PNG Information
IMG. getrgb (0, 0, IMG. getwidth (), IMG. getheight (), RGB, 0,
IMG. getwidth ());
// A new PNG
Logobufferedimage = new bufferedimage (IMG. getwidth (),
IMG. getheight (), bufferedimage. type_int_argb );
// Set Data
Logobufferedimage. setrgb (0, 0, IMG. getwidth (), IMG. getheight (),
RGB, 0, IMG. getwidth ());
System. Out. println ("logo is init OK .");
} Catch (ioexception e ){
E. printstacktrace ();
}
}
Public static bufferedimage logobufferedimage;
/**
* Read transparent PNG watermarks
*/
Public static int [] readlogo (){
Bufferedimage IMG;
Try {
// Read transparent PNG
IMG = ImageIO. Read (new file (logoapng ));
// Generate an array for storing PNG data
Int RGB [] = new int [img. getwidth () * IMG. getheight ()];
// Read PNG Information
IMG. getrgb (0, 0, IMG. getwidth (), IMG. getheight (), RGB, 0,
IMG. getwidth ());
// A new PNG
Bufferedimage imgnew = new bufferedimage (IMG. getwidth (),
IMG. getheight (), bufferedimage. type_int_argb );
// Set Data
Imgnew. setrgb (0, 0, IMG. getwidth (), IMG. getheight (), RGB, 0,
IMG. getwidth ());
Return RGB;
} Catch (ioexception e ){
E. printstacktrace ();
Return NULL;
}
}
/**
* Read transparent PNG watermarks
*/
Public static void readapng (){
Bufferedimage IMG;
Try {
// Read transparent PNG
IMG = ImageIO. Read (new file (logoapng ));
// Generate an array for storing PNG data
Int RGB [] = new int [img. getwidth () * IMG. getheight ()];
// Read PNG Information
IMG. getrgb (0, 0, IMG. getwidth (), IMG. getheight (), RGB, 0,
IMG. getwidth ());
// A new PNG
Bufferedimage imgnew = new bufferedimage (IMG. getwidth (),
IMG. getheight (), bufferedimage. type_int_argb );
// Set Data
Imgnew. setrgb (0, 0, IMG. getwidth (), IMG. getheight (), RGB, 0,
IMG. getwidth ());
// Save it to the new file half.png
ImageIO. Write (imgnew, "PNG", new file (logoapngtest ));
} Catch (ioexception e ){
E. printstacktrace ();
}
}
}