本文整理匯總了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());
}
}
}