本文整理汇总了Java中net.coobird.thumbnailator.Thumbnails类的典型用法代码示例。如果您正苦于以下问题:Java Thumbnails类的具体用法?Java Thumbnails怎么用?Java Thumbnails使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Thumbnails类属于net.coobird.thumbnailator包,在下文中一共展示了Thumbnails类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: processProfileImage
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
public void processProfileImage(String providerImageUrl, String userKey)
throws IOException {
// Reduce original image size. Thumbnailator will not modify
// image if less than 600x600
BufferedImage bufferedProfileImage =
Thumbnails.of(new URL(providerImageUrl))
.forceSize(600, 600)
.allowOverwrite(true)
.outputFormat("png")
.asBufferedImage();
saveProfileImage(bufferedProfileImage, userKey, false);
// Create profile image icon. Saved to separate directory
BufferedImage bufferedIconImage =
Thumbnails.of(new URL(providerImageUrl))
.forceSize(32, 32)
.allowOverwrite(true)
.outputFormat("png")
.asBufferedImage();
saveProfileImage(bufferedIconImage, userKey, true);
}
示例2: processRequest
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
String templateName = request.getParameter("demo_name");
Part filePart = request.getPart("demo_img");
try{
// String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
InputStream fileContent = filePart.getInputStream();
System.out.println(templateName);
File file = new File("uploads/"+templateName+".png");
System.out.println(file.getAbsolutePath());
OutputStream outputStream = new FileOutputStream(file);
IOUtils.copy(fileContent, outputStream);
outputStream.close();
Thumbnails.of(new File("uploads/"+templateName+".png"))
.size(500, 707)
.outputFormat("PNG")
.toFiles(Rename.NO_CHANGE);
}catch(NullPointerException ex){
System.out.println("null param");
}
response.getWriter().write("uploaded lmao");
System.out.println("fking shit");
response.sendRedirect("init.jsp?req="+templateName);
}
示例3: scaleImg
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
/**
* 生成缩略图
* @param file
* @param suffix
* @param width
* @param height
* @throws IOException
*/
public static void scaleImg(File file,String suffix,int width,int height) throws IOException{
if(file.isDirectory()){
File[] subFiles = file.listFiles();
for (File subFile : subFiles) {
scaleImg(subFile, suffix, width, height);
}
}else{
double length = file.length();
String path = file.getAbsolutePath();
int cut = path.lastIndexOf(".");
String prefixFile = StringUtils.substring(path, 0, cut);
String formatFile = StringUtils.substring(path, cut,path.length());
String scaleFileName = prefixFile+suffix+formatFile;
double quality = 1;
if(length>=100*1000){
quality = 0.8;
}
Thumbnails.of(file).allowOverwrite(true).size(width,height).outputQuality(quality).toFile(scaleFileName);
}
}
示例4: generateThumImage
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
/**
* 缩放并裁剪图片,输出缩放后的图片和缩放后裁剪的图片。
* @param inPath 输入图片地址
* @param outPath 缩放输出地址
* @param outPath2 裁剪输出地址
*/
public PageData generateThumImage(String inPath, String outPath, String outPath2) {
try {
BufferedImage read = ImageIO.read(new File(inPath));
int height = read.getHeight();
int width = read.getWidth();
if (height > width) {
Thumbnails.of(inPath).width(this.changeWeight)
.toFile(outPath);
}
if (height < width) {
Thumbnails.of(inPath).height(this.changeHeight)
.toFile(outPath);
}
if (height == width) {
Thumbnails.of(inPath).forceSize(this.changeHeight, this.changeWeight)
.toFile(outPath);
}
CutImg(outPath, outPath2, height);
} catch (IOException e) {
e.printStackTrace();
return WebResult.requestFailed(400, "图片压缩失败", null);
}
return WebResult.requestSuccess();
}
示例5: CutImg
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
/**
* 如果设置长图的长度不为0而且传入的图片高度大于设置长图的长度。就认为是长图,裁剪中心顶部,添加长图的水印。
* @param outPath 输入路径
* @param outPath2 输出路径
* @param height 传入图片的高度
*/
private void CutImg(String outPath, String outPath2, int height) {
try {
if (this.longImageHeigh != 0 && height >= this.longImageHeigh) {
Thumbnails.of(outPath).sourceRegion(Positions.TOP_CENTER, this.changeHeight, this.changeWeight)
.size(this.changeHeight, this.changeWeight)
.outputQuality(1d).watermark(Positions.BOTTOM_RIGHT, ImageIO.read(this.waterImageFile), 1f)
.toFile(outPath2);
} else {
Thumbnails.of(outPath).sourceRegion(Positions.CENTER, this.changeHeight, this.changeWeight)
.size(this.changeHeight, this.changeWeight)
.outputQuality(1d)
.toFile(outPath2);
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例6: small
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
protected byte[] small(long uid, String uri, String size, byte[] data) throws IOException {
String[] list = size.split("x");
int width = Integer.parseInt(list[0]);
int height = Integer.parseInt(list[1]);
ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayInputStream input = new ByteArrayInputStream(data);
Thumbnails.of(input).size(width, height).toOutputStream(output);
// http://rensanning.iteye.com/blog/1545708 Java生成缩略图之Thumbnailator
// Thumbnails.of(input).asBufferedImage();
// ImageIO.getImageReader(writer)
String uri2 = uri.replaceFirst(".jpg", "_" + size + ".jpg");
byte[] data2 = output.toByteArray();
if (data2.length <= 0) {
throw new FileNotFoundException("怎么压缩后的图片会为空[" + uri2 + "]?");
}
dfsService.write(uri2, data2, uid);
return data2;
}
示例7: crop
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
protected byte[] crop(long uid, String uri, String size, byte[] data) throws IOException {
if (data.length <= 0) {
throw new IllegalArgumentException("图片文件不能为空.");
}
String[] list = size.split("x");
int width = Integer.parseInt(list[0]);
int height = Integer.parseInt(list[1]);
ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayInputStream input = new ByteArrayInputStream(data);
// Thumbnails.of(input).size(width, height).toOutputStream(output);
Thumbnails.of(input).crop(Positions.CENTER).size(width, height).keepAspectRatio(true).toOutputStream(output);
// http://rensanning.iteye.com/blog/1545708 Java生成缩略图之Thumbnailator
// Thumbnails.of(input).asBufferedImage();
// ImageIO.getImageReader(writer)
String uri2 = uri.replaceFirst(".jpg", "_" + size + "_crop.jpg");
byte[] data2 = output.toByteArray();
dfsService.write(uri2, data2, uid);
return data2;
}
示例8: crop
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
/**
* 图像切割(按指定起点坐标和宽高切割)
*/
@SneakyThrows
public static ByteArrayOutputStream crop(@Nonnull InputStream inputStream, int x, int y, int width, int height) {
checkNotNull(inputStream);
checkArgument(x >= 0);
checkArgument(y >= 0);
checkArgument(width > 0);
checkArgument(height > 0);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Thumbnails.of(inputStream)
.sourceRegion(Positions.TOP_LEFT, width, height)
.size(width, height)
.outputFormat(IMAGE_TYPE_JPG)
.toOutputStream(baos);
return baos;
}
示例9: calculateMaxSizeInFixedAspectRatio
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
/**
* 计算最大宽高
*/
@SneakyThrows
public static TcPair<Integer, Integer> calculateMaxSizeInFixedAspectRatio(@Nonnull InputStream inputStream,
double ratio) {
checkNotNull(inputStream);
checkArgument(ratio > 0);
BufferedImage img = Thumbnails.of(inputStream).scale(1).asBufferedImage();
int width = img.getWidth();
int height = img.getHeight();
double imgRatio = (double) width / (double) height;
if (imgRatio > ratio) {
width = (int) ((double) width * ratio / imgRatio);
} else {
height = (int) ((double) height * imgRatio / ratio);
}
return TcPair.with(width, height);
}
示例10: visualizeMulti
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
@Override
protected String visualizeMulti(List<Map<String, PrimitiveTypeProvider>> featureData){
BufferedImage image = new BufferedImage(featureData.size(), 1, BufferedImage.TYPE_INT_RGB);
int count = 0;
for (Map<String, PrimitiveTypeProvider> feature : featureData) {
int[][] avg = ArtUtil.shotToInt(feature.get("feature").getFloatArray(), 1, 1);
image.setRGB(count, 0, avg[0][0]);
count++;
}
try {
image = Thumbnails.of(image).scalingMode(ScalingMode.BILINEAR).scale(10, 1).asBufferedImage();
} catch (IOException e) {
e.printStackTrace();
}
return ImageParser.BufferedImageToDataURL(image, "png");
}
示例11: classifyImage
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
/**
* Classifies an Image with the given neural net.
* Performs 3 Classifications with different croppings, maxpools the vectors on each dimension to get hits
*/
private float[] classifyImage(BufferedImage img) {
float[] probs = new float[1000];
Arrays.fill(probs, 0f);
Position[] positions = new Position[3];
positions[0] = Positions.CENTER;
if (img.getHeight() > img.getWidth()) {
positions[1] = Positions.TOP_RIGHT;
positions[2] = Positions.BOTTOM_RIGHT;
} else {
positions[1] = Positions.CENTER_RIGHT;
positions[2] = Positions.CENTER_LEFT;
}
float[] curr;
for (Position pos : positions) {
try {
curr = getNet().classify(Thumbnails.of(img).size(224, 224).crop(pos).asBufferedImage());
probs = NeuralNetUtil.maxpool(curr, probs);
} catch (IOException e) {
LOGGER.error(e);
}
}
return probs;
}
示例12: thumbBySizeWithoutScale
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
/**
*
*@name 根据指定长度和宽度生成缩略图,不维持原宽高比例
*@Description 相关说明
*@Time 创建时间:2014-7-22上午9:30:11
*/
public static byte[] thumbBySizeWithoutScale(byte[] content,int width,int length){
InputStream inputStream = new ByteArrayInputStream(content);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
Thumbnails
.of(inputStream)
.size(width,length)
.keepAspectRatio(false)
.toOutputStream(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
return outputStream.toByteArray();
}
示例13: compressTo
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
public String compressTo(String imagePath, String targetPath){
if(!isCompressFile(imagePath)){
throw new BaseException("not support compress file type: " + FileUtils.getExtendName(imagePath));
}
Builder<File> builder = Thumbnails.fromFilenames(Arrays.asList(imagePath));
configBuilder(builder, config);
File targetFile = null;
if(StringUtils.isBlank(targetPath)){
File imageFile = new File(imagePath);
String fileName = FileUtils.getFileNameWithoutExt(imageFile.getName()) + "-compress" + FileUtils.getExtendName(imageFile.getName(), true);
fileName = FileUtils.newFileNameAppendRepeatCount(imageFile.getParent(), fileName);
targetFile = new File(imageFile.getParentFile(), fileName);
}else{
targetFile = new File(targetPath);
}
try {
builder.toFile(targetFile);
} catch (IOException e) {
throw new BaseException("compress image error: " + e.getMessage(), e);
}
return targetPath;
}
示例14: main
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
public static void main(String[] args) {
String filePath = "D:\\tomca7\\webapps\\unique-img-plugin\\upload\\12674158787444.jpg";
File img = new File("D:\\tomca7\\webapps\\unique-img-plugin\\upload\\12674158787444_1.jpg");
Builder<File> f = Thumbnails.of(filePath);
f.size(200, 200);
// f = f.scale(1);
// f = f.outputQuality(quality);
if ("a".equals("a")) {
f.rotate(180);
}
try {
f.toFile(img);
} catch (IOException e) {
logger.warn(e.getMessage());
}
}
示例15: resize
import net.coobird.thumbnailator.Thumbnails; //导入依赖的package包/类
public void resize(File folder, File outFolder) {
if (!outFolder.exists()) {
outFolder.mkdirs();
}
for (File file : folder.listFiles()) {
List<File> files = new ArrayList<File>();
files.add(file);
try {
Thumbnails
.fromFiles(files)
.size(400, 400)
.toFile(outFolder.getPath() + File.separator
+ file.getName());
} catch (Exception e) {
System.err.println(file.getName());
}
}
}