The actual development of picture compression in proportion

com.lufax.g.media.utils.Logger Import; 
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.stereotype.Service;

Import javax.imageio.ImageIO;
Import java.awt.image.BufferedImage ;
Import the java.io. *;

/ **
* image compression
* the Created by shazhipeng880 ON 19-10-11.
* /
@Service
public class CompressImgService {

public compressImgByWidth the inputStream (the inputStream inputStream, Integer width) {

the OutputStream OS = null;
the inputStream = null IS;
String fileName = "1.jpg";

// save the image to a temporary path
savePic (inputStream, fileName);

// after thumbnail picture temporary storage address
Where do you want the System.getProperty = String ( "the java.io.tmpdir") + + fileName the File.separator;

the try {
the BufferedImage Image ImageIO.read = (new new the FileInputStream (Where do you want));

OS = new new a FileOutputStream (Where do you want);

// picture width
int srcWidth image.getWidth = ();
// picture height
int srcHeight = image.getHeight ();
// after abbreviated width
int compressWidth width.intValue = ();
// abbreviated ratio
int rate = srcWidth / compressWidth ;

// after calculating the height of the thumbnail
int compressHeight = srcHeight / rate;

BufferedImage bufferedImage = new BufferedImage(compressWidth, compressHeight, BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(image.getScaledInstance(compressWidth, compressHeight, image.SCALE_SMOOTH), 0, 0, null);

String imageType = "jpg";
ImageIO.write(bufferedImage, imageType, os);

//缩略后图片的流
is = new FileInputStream(destination);

} catch (FileNotFoundException e) {

} catch (IOException e) {

} finally {
if(null != os){
try {
os.close();
} catch (IOException e) {

}
}

// delete temporary files
deleteFile (Where do you want);

return IS;
}
}

public void savePic (inputStream InputStream, String fileName) {
OutputStream os = null;
the try {
String path = System.getProperty ( "java.io.tmpdir");
// 2, saved to a temporary file
// 1K data buffer
byte [] = new new BS byte [1024];
// read data length
int len;
file save // output stream to a local file

file tempFile = new File (path);
IF {(tempFile.exists ()!)
tempFile.mkdirs ();
}
os = new FileOutputStream(tempFile.getPath() + File.separator + fileName);
int available = inputStream.available();
// 开始读取
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}

} catch (IOException e) {

} catch (Exception e) {

} finally {
// 完毕,关闭所有链接
if(null != inputStream){
try {
inputStream.close();
os.close();
} catch (IOException e) {

}
}
}
}

Boolean the deleteFile public (String fileName) { 
File File = new new File (fileName);
// if the file corresponding to the file path exists and is a file, delete
if (file.exists () && file.isFile ( )) {
IF (File.delete ()) {
System.out.println ( "delete a single file" + fileName + "success!");
return to true;
} the else {
System.out.println ( "delete a single file" + fileName + ! "failure");
return to false;
}
} the else {
System.out.println ( "failed to delete a single file:" + fileName + "does not exist!");
return to false;
}
}


}

Guess you like

Origin www.cnblogs.com/it-szp/p/11672421.html