Package *;
Import Java.awt.Color;
Import Java.awt.Graphics2D;
Import Java.awt.Image;
Import Java.awt.image.BufferedImage;
Import Java.io.File;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.ArrayList;
Import java.util.List;
Import Javax.imageio.ImageIO;
Import Javax.swing.ImageIcon;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGEncodeParam;
Import Com.sun.image.codec.jpeg.JPEGImageEncoder;
public class Imagetest {
Public imagetest () {}
public static void Main (string[] args) {
list<string> bookfilepaths = new arraylist<string> ();
Bookfilepaths.add ("e:\\qq\\1.jpg");
Bookfilepaths.add ("e:\\qq\\2.jpg");
Bookfilepaths.add ("e:\\qq\\3.jpg");
Bookfilepaths.add ("e:\\qq\\4.jpg");
Bookfilepaths.add ("e:\\qq\\5.jpg");
Bookfilepaths.add ("e:\\qq\\6.jpg");
Bookfilepaths.add ("e:\\qq\\7.jpg");
Bookfilepaths.add ("e:\\qq\\8.jpg");
Bookfilepaths.add ("e:\\qq\\10.jpg");
String picname = "E:\\qq\\test1.jpg";
for (String jpg:bookfilepaths) {
try {
Crop picture 200*200
Dofiledownload (jpg,
NULL, 200, 200);
} catch (Exception e) {
E.printstacktrace ();
}
}
Multiple small pictures generated nine picture 600*600
Createbigjpg (Bookfilepaths,
Color.gray, Picname);
}
/**
* Draw Nine Gongge pictures
* @param smalljpg
* @param bgColor
* @param picname
*/
private static void Createbigjpg (List<string> smalljpg,
Color BgColor, String picname) {
try {
int imagecount = Smalljpg.size ();
Height, width of each small picture
int smallwidth = 200;
int smallheight = 200;
Draw a background image according to the big picture width height
int setwidth = 600;
int setheight = 600;
BufferedImage bufimage = new BufferedImage (SetWidth, SetHeight,
BUFFEREDIMAGE.TYPE_INT_RGB);
Graphics2D g = bufimage.creategraphics ();
G.setcolor (BgColor); The color of the background
G.fillrect (0, 0, setwidth, setheight);
int rowCount = GetRowCount (Imagecount);
int leftmargin[] = {200,100,0};//left margin
int topmargin[] = {200,100,0};//left margin
int len = 0;
int y = topmargin[rowcount-1]; Ordinate
for (int i = 1; I <= rowCount; i++) {//Traverse each line
int colcount = Getcolcount (Imagecount, RowCount, i);
int x = leftmargin[colcount-1]; The horizontal axis may appear on the left margin
for (int j = 1; J <= ColCount; j + +) {
String jpgname = Smalljpg.get (len);
ImageIcon icon = new ImageIcon (jpgname);
Image img = icon.getimage ();
G.drawimage (IMG, x, y, null);
x + = Smallwidth;
len++;
}
Y + = Smallheight;
}
G.dispose ();
FileOutputStream out = new FileOutputStream (picname); Specify Output file
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out); Set file format
JPEGEncodeParam param = Encoder.getdefaultjpegencodeparam (bufimage); Read from the picture buffer
Param.setquality (50f, true);
Encoder.encode (Bufimage, param); Save
Out.flush ();
Out.close ();
} catch (Exception e) {
System.out.println ("Createbigjpg failed!");
E.printstacktrace ();
}
}
Get total number of rows
public static int GetRowCount (int imagecount) {
if (Imagecount > 6) {
return 3;
}
if (Imagecount > 3) {
return 2;
}
return 1;
}
Gets the number of columns for the current row
public static int Getcolcount (int imagecount, int rowcount, int currentrow) {
if (rowcount = = 2) {//Total rows
if (CurrentRow = = 1) {//when forward
return imageCount-3;
}else{
return 3;
}
}
if (rowcount = = 3) {
if (CurrentRow = = 1) {
return imageCount-6;
}else{
return 3;
}
}
return imagecount;
}
Crop pictures
public static void Dofiledownload (String path,
String filename, int width, int hight)
Throws Exception {
Path refers to the paths of the files you want to download.
File File = new file (path);
Image img = imageio.read (file); Constructing an Image Object
int srcwidth = Img.getwidth (null); Get the source map width
int srcheight = Img.getheight (null); Get the source graph length
Compression width or height max width hight max height
if (Srcwidth/srcheight > Width/hight) {
int h = (int) (srcheight * width/srcwidth);
Resize (img, width, h, path, file);
} else {
int w = (int) (srcwidth * hight/srcheight);
Resize (IMG, W, hight, path, file);
}
}
/**
* Force compress/Enlarge picture to fixed size
*
* @param W
* Int new width
* @param h
* Int new Height
*/
public static void Resize (Image img, int w, int h,
String path, file file)
Throws IOException {
Scale_smooth's thumbnail algorithm produces thumbnail images with higher priority than high-speed image quality but slower
BufferedImage image = New BufferedImage (W, H,
BUFFEREDIMAGE.TYPE_INT_RGB);
Image.getgraphics (). DrawImage (img, 0, 0, W, h, NULL); Draw a zoomed-out diagram
File DestFile = new file (path);
FileOutputStream out = new FileOutputStream (destfile); Output to file stream
BMP, PNG, GIF to JPG can be implemented normally
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (out);
Encoder.encode (image); JPEG encoding
Out.close ();
}
}
Draw Nine Gongge Pictures