Java automatically generates thumbnail diagram example code _java

Source: Internet
Author: User

When you want to do a library project, the picture size, pixel control is the first problem to be solved.

One, the single graph produces slightly shrinks the graph
Dantu is redrawn to generate a new picture. The new diagram can be reduced by a certain percentage from the old picture, or it can be defined as a fixed size.
The detailed code is as follows:

Copy Code code as follows:

<span style= "font-size:14px" >import com.sun.image.codec.jpeg.JPEGImageEncoder;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGEncodeParam;
Import Javax.imageio.ImageIO;
Import Java.awt.image.BufferedImage;
Import Java.util.HashMap;
Import java.util.List;
Import java.util.ArrayList;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.FileOutputStream;
Import Java.util.Map;
public class Picchange {
/**
* @param im RAW image
* @param resizetimes need to reduce the number of multiples, twice times the original 1/2, the larger the value, the smaller the return of the picture
* @return return the processed image
*/
Public BufferedImage resizeimage (BufferedImage im, float resizetimes) {
/* The width and height of the original image * *
int width = im.getwidth ();
int height = im.getheight ();
/* The width and height of the adjusted picture * *
int towidth = (int) (Float.parsefloat (string.valueof (width))/resizetimes);
int toheight = (int) (Float.parsefloat (string.valueof (height))/resizetimes);
/* New Generated results picture * *
BufferedImage result = new BufferedImage (Towidth, Toheight, Bufferedimage.type_int_rgb);
Result.getgraphics (). DrawImage (Im.getscaledinstance (Towidth, Toheight, Java.awt.Image.SCALE_SMOOTH), 0, 0, NULL);
return result;
}
/**
* @param im RAW image
* @param resizetimes multiples, such as 0.5 is reduced by half, 0.98, etc. double type
* @return return the processed image
*/
Public BufferedImage zoomimage (BufferedImage im, float resizetimes) {
/* The width and height of the original image * *
int width = im.getwidth ();
int height = im.getheight ();
/* The width and height of the adjusted picture * *
int towidth = (int) (Float.parsefloat (string.valueof (width)) * resizetimes);
int toheight = (int) (Float.parsefloat (string.valueof (height)) * resizetimes);
/* New Generated results picture * *
BufferedImage result = new BufferedImage (Towidth, Toheight, Bufferedimage.type_int_rgb);
Result.getgraphics (). DrawImage (Im.getscaledinstance (Towidth, Toheight, Java.awt.Image.SCALE_SMOOTH), 0, 0, NULL);
return result;
}
public boolean writehighquality (BufferedImage im, String filefullpath) {
try {
/* Output to file stream * *
FileOutputStream newimage = new FileOutputStream (Filefullpath+system.currenttimemillis () + ". jpg");
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (newimage);
JPEGEncodeParam Jep = Jpegcodec.getdefaultjpegencodeparam (IM);
/* Compression Quality * *
Jep.setquality (1f, true);
Encoder.encode (IM, Jep);
/* Near JPEG coding * *
Newimage.close ();
return true;
catch (Exception e) {
return false;
}
}
public static void Main (string[] args) throws exception{
String Inputfoler = "F:\\pic";
/* Here fill in your folder to reduce the picture of the full address * *
String OutputFolder = "f:\\picnew\\";
* * Here to fill in your converted pictures stored folder/
float times = 0.25f;
/* This parameter is to be converted to a multiple, if it is 1 is converted to 1 time times * *
Picchange r = new Picchange ();
File FF = new file ("F:\\pic\\chrysanthemum1.jpg");
BufferedImage f = javax.imageio.ImageIO.read (FF);
R.writehighquality (R.zoomimage (f,times), OutputFolder);

}
}</span>

When you move the above code to MyEclipse, you may have an error introducing the toolkit.
Copy Code code as follows:

<span style= "font-size:14px" >import com.sun.image.codec.</span>

workaround: Just put the forbidden reference in the deprecated and restricted APIs inside the Windows-preferences-java-compiler-errors/warnings s (access rules) is selected as warning and can be compiled.

Second, batch generation of slightly reduced image
Batch generation thumbnail to convert files in a known folder to. jpg or other picture suffixes and put them in a separate folder

Copy Code code as follows:

<span style= "font-size:14px" >import com.sun.image.codec.jpeg.JPEGImageEncoder;
Import Com.sun.image.codec.jpeg.JPEGCodec;
Import Com.sun.image.codec.jpeg.JPEGEncodeParam;
Import Javax.imageio.ImageIO;
Import Java.awt.image.BufferedImage;
Import Java.util.HashMap;
Import java.util.List;
Import java.util.ArrayList;
Import Java.io.File;
Import java.io.IOException;
Import Java.io.FileOutputStream;
Import Java.util.Map;
public class Resizeimage {
/**
* @param im RAW image
* @param resizetimes need to reduce the number of multiples, twice times the original 1/2, the larger the value, the smaller the return of the picture
* @return return the processed image
*/
Public BufferedImage resizeimage (BufferedImage im, float resizetimes) {
/* The width and height of the original image * *
int width = im.getwidth ();
int height = im.getheight ();
/* The width and height of the adjusted picture * *
int towidth = (int) (Float.parsefloat (string.valueof (width))/resizetimes);
int toheight = (int) (Float.parsefloat (string.valueof (height))/resizetimes);
/* New Generated results picture * *
BufferedImage result = new BufferedImage (Towidth, Toheight, Bufferedimage.type_int_rgb);
Result.getgraphics (). DrawImage (Im.getscaledinstance (Towidth, Toheight, Java.awt.Image.SCALE_SMOOTH), 0, 0, NULL);
return result;
}
/**
* @param im RAW image
* @param resizetimes multiples, such as 0.5 is reduced by half, 0.98, etc. double type
* @return return the processed image
*/
Public BufferedImage zoomimage (BufferedImage im, float resizetimes) {
/* The width and height of the original image * *
int width = im.getwidth ();
int height = im.getheight ();
/* The width and height of the adjusted picture * *
int towidth = (int) (Float.parsefloat (string.valueof (width)) * resizetimes);
int toheight = (int) (Float.parsefloat (string.valueof (height)) * resizetimes);
/* New Generated results picture * *
BufferedImage result = new BufferedImage (Towidth, Toheight, Bufferedimage.type_int_rgb);
Result.getgraphics (). DrawImage (Im.getscaledinstance (Towidth, Toheight, Java.awt.Image.SCALE_SMOOTH), 0, 0, NULL);
return result;
}
/**
* @param the folder where you want to convert the image, the folder path where the image is stored
* @param an array of the suffix names of the type pictures
* @return
*/
Public list<bufferedimage> getimagelist (String path, string[] type) throws ioexception{
map<string,boolean> map = new hashmap<string, boolean> ();
for (String S:type) {
Map.put (s,true);
}
list<bufferedimage> result = new arraylist<bufferedimage> ();
file[] filelist = new File (path). Listfiles ();
for (File f:filelist) {
if (f.length () = = 0)
Continue
if (Map.get (GetExtension (f.getname)) = = null)
Continue
Result.add (Javax.imageio.ImageIO.read (f));
}
return result;
}
/**
* Write the picture to the disk
* @param im
* @param the folder address written by the path eg:c://home//picture
* @param fileName Dcm1987.jpg Write the name of the picture
* @return
*/
public boolean Writetodisk (BufferedImage im, string path, string fileName) {
File F = new file (path + fileName);
String FileType = getextension (fileName);
if (FileType = null)
return false;
try {
Imageio.write (IM, fileType, f);
Im.flush ();
return true;
catch (IOException e) {
return false;
}
}
public boolean writehighquality (BufferedImage im, String filefullpath) {
try {
/* Output to file stream * *
FileOutputStream newimage = new FileOutputStream (Filefullpath+system.currenttimemillis () + ". jpg");
JPEGImageEncoder encoder = Jpegcodec.createjpegencoder (newimage);
JPEGEncodeParam Jep = Jpegcodec.getdefaultjpegencodeparam (IM);
/* Compression Quality * *
Jep.setquality (1f, true);
Encoder.encode (IM, Jep);
/* Near JPEG coding * *
Newimage.close ();
return true;
catch (Exception e) {
return false;
}
}
/**
* Returns the file suffix name of the file
* @param fileName
* @return
*/
public string GetExtension (string fileName) {
try {
return Filename.split ("\.") [Filename.split ("\."). LENGTH-1];
catch (Exception e) {
return null;
}
}
public static void Main (string[] args) throws exception{
String Inputfoler = "F:\\pic";
/* Here fill in your folder to reduce the picture of the full address * *
String OutputFolder = "f:\\picnew\\";
* * Here to fill in your converted pictures stored folder/
float times = 0.25f;
/* This parameter is to be converted to a multiple, if it is 1 is converted to 1 time times * *
Resizeimage r = new Resizeimage ();
list<bufferedimage> imageList = r.getimagelist (inputfoler,new string[] {"JPG"});
for (BufferedImage i:imagelist) {
R.writehighquality (R.zoomimage (i,times), OutputFolder);
}
}
}</span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.