package com.boot.security.server.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;
public class PicUtils{
private static float QUALITY = 0.8f;
public static byte[] compressOfSize(File file, int width, int height)
throws Exception {
byte[] bs = null;
InputStream input = null;
try {
input = new FileInputStream(file);
bs = compressOfSize(input, width, height);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (input != null)
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bs;
}
public static byte[] compressOfSize(InputStream input, int width, int height)
throws Exception {
ByteArrayOutputStream output = null;
try {
output = new ByteArrayOutputStream();
Thumbnails.of(input).size(width, height).toOutputStream(output);
return output.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static byte[] compressOfPercent(InputStream input, float percent)
throws Exception {
ByteArrayOutputStream output = null;
try {
output = new ByteArrayOutputStream();
Thumbnails.of(input).scale(percent).toOutputStream(output);
return output.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static byte[] compressOfPercent(File file, float percent)
throws Exception {
byte[] bs = null;
InputStream input = null;
try {
input = new FileInputStream(file);
bs = compressOfPercent(input, percent);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (input != null)
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bs;
}
public static byte[] compressOfQuality(File file, float quality)
throws Exception {
byte[] bs = null;
InputStream input = null;
try {
input = new FileInputStream(file);
bs = compressOfQuality(input, quality);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (input != null)
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return bs;
}
public static byte[] compressOfQuality(InputStream input, float quality)
throws Exception {
ByteArrayOutputStream output = null;
try {
output = new ByteArrayOutputStream();
if(quality == 0){
Thumbnails.of(input).scale(1f).outputQuality(QUALITY)
.toOutputStream(output);
} else {
Thumbnails.of(input).scale(1f).outputQuality(quality)
.toOutputStream(output);
}
return output.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static byte[] waterMark(byte[] fromPic, InputStream markPic,
float transparent) throws Exception {
InputStream finput = null;
ByteArrayOutputStream output = null;
try {
finput = new ByteArrayInputStream(fromPic);
output = new ByteArrayOutputStream();
Thumbnails
.of(finput)
.scale(1f)
.watermark(Positions.BOTTOM_RIGHT, ImageIO.read(markPic),
transparent).toOutputStream(output);
return output.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
if (finput != null)
finput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static byte[] transferPicFormat(byte[] fromPic, String picFormat)
throws Exception {
ByteArrayInputStream finput = null;
ByteArrayOutputStream output = null;
try {
finput = new ByteArrayInputStream(fromPic);
output = new ByteArrayOutputStream();
Thumbnails.of(finput).outputFormat(picFormat)
.toOutputStream(output);
return output.toByteArray();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (output != null)
output.close();
if (finput != null)
finput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static byte[] compressPicForScale(byte[] imageBytes, long desFileSize) {
if (imageBytes == null || imageBytes.length <= 0 || imageBytes.length < desFileSize * 1024) {
return imageBytes;
}
long srcSize = imageBytes.length;
double accuracy = getAccuracy(srcSize / 1024);
try {
while (imageBytes.length > desFileSize * 1024) {
ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length);
Thumbnails.of(inputStream)
.scale(accuracy)
.outputQuality(accuracy)
.toOutputStream(outputStream);
imageBytes = outputStream.toByteArray();
}
} catch (Exception e) {
e.printStackTrace();
}
return imageBytes;
}
private static double getAccuracy(long size) {
double accuracy;
if (size < 900) {
accuracy = 0.85;
} else if (size < 2047) {
accuracy = 0.6;
} else if (size < 3275) {
accuracy = 0.44;
} else {
accuracy = 0.4;
}
return accuracy;
}
}
记录一下.



