// 指定大小进行缩放 private void test1() throws Exception { // size(宽度, 高度) /* * 若图片横比200小,高比300小,不变 若图片横比200小,高比300大,高缩小到300,图片比例不变 * 若图片横比200大,高比300小,横缩小到200,图片比例不变 若图片横比200大,高比300大,图片按比例缩小,横为200或高为300 */ Thumbnails.of(getImageStream()).size(200, 300).toFile(ouputImagePath("a380_200x300.jpg")); Thumbnails.of(getImageStream()).size(2560, 2048).toFile(ouputImagePath("a380_2560x2048.jpg")); }
// 转化图像格式 private void test7() throws Exception { // outputFormat(图像格式) Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .outputFormat("png") .toFile("img/a380_1280x1024.png"); Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .outputFormat("gif") .toFile("img/a380_1280x1024.gif"); }
// 旋转 private void test4() throws Exception { // rotate(角度),正数:顺时针 负数:逆时针 Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .rotate(90) .toFile("img/a380_rotate+90.jpg"); Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .rotate(-90) .toFile("img/a380_rotate-90.jpg"); }
// 水印 private void test5() throws Exception { // watermark(位置,水印图,透明度) Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .watermark(Positions.BOTTOM_RIGHT, ImageIO.read(new File("images/watermark.png")), 0.5f) .outputQuality(0.8f) .toFile("a380_watermark_bottom_right.jpg"); Thumbnails.of("images/a380_1280x1024.jpg") .size(1280, 1024) .watermark(Positions.CENTER, ImageIO.read(new File("images/watermark.png")), 0.5f) .outputQuality(0.8f) .toFile("a380_watermark_center.jpg"); }
// 不按照比例,指定大小进行缩放 private void test3() throws Exception { // keepAspectRatio(false) 默认是按照比例缩放的 Thumbnails.of("images/a380_1280x1024.jpg") .size(200, 200) .keepAspectRatio(false) .toFile("img/a380_200x200.jpg"); }
public void finishProcess() { try { boolean wl = true; new File("./engine/temp/resize").mkdir(); while (wl == true) { File file = new File("./engine/temp/belt_img_10.jpg"); if (file.exists() == true) { Thumbnails.of( "./engine/temp/belt_img_1.jpg", "./engine/temp/belt_img_2.jpg", "./engine/temp/belt_img_3.jpg", "./engine/temp/belt_img_4.jpg", "./engine/temp/belt_img_5.jpg", "./engine/temp/belt_img_6.jpg", "./engine/temp/belt_img_7.jpg", "./engine/temp/belt_img_8.jpg", "./engine/temp/belt_img_9.jpg", "./engine/temp/belt_img_10.jpg") .size(256, 192) .outputQuality(1.0) .toFiles(new File("./engine/temp/resize/"), Rename.NO_CHANGE); jButton1.setEnabled(true); wl = false; } } } catch (Exception e) { } }
@Override public void saveAvatar(String userName, InputStream originalIs) throws AvatarUploadException { ByteArrayOutputStream os = null; InputStream is = null; try { BufferedImage originalImage = ImageIO.read(originalIs); BufferedImage thumbnail = Thumbnails.of(originalImage).crop(Positions.CENTER).size(128, 128).asBufferedImage(); os = new ByteArrayOutputStream(); ImageIO.write(thumbnail, "png", os); is = new ByteArrayInputStream(os.toByteArray()); String fileName = String.format(AVATAR_NAME, userName); GridFS avatarFS = new GridFS(m_template.getDb()); avatarFS.remove(fileName); GridFSInputFile gfsFile = avatarFS.createFile(is); gfsFile.setFilename(fileName); gfsFile.save(); } catch (Exception ex) { throw new AvatarUploadException(ex); } finally { IOUtils.closeQuietly(originalIs); IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } }
/** * ����ͼƬ * * @param filePath ԴͼƬλ�� * @param thumbPath ���Ժ��λ�� * @param width ���Կ� * @param height ���Ը� * @param scale ���������� * @param quality ͼƬ�����ٷ��� * @param rotate ��ת�Ƕ� * @return */ public static String thumb( String filePath, String thumbPath, int width, int height, double scale, double quality, double rotate) { File img = new File(thumbPath); if (img.exists()) { return img.getPath(); } Builder<File> f = Thumbnails.of(filePath); if (width > 0 && height > 0) { f.size(width, height); } if (scale > 0.0D) { f.scale(scale); } if (quality > 0.0D) { f.outputQuality(quality); } if (rotate > 0.0D) { f.rotate(rotate); } try { f.toFile(img); return img.getPath(); } catch (IOException e) { e.printStackTrace(); } return filePath; }
private void makeThumbnailImage(String originPath, String thumbPath) throws IOException { Thumbnails.of(new File(originPath)) .size(60, 44) .outputFormat("png") .outputQuality(1.0) .toFile(new File(thumbPath)); }
@Test public void test() throws Exception { File srcFile = new File("C:/大图.jpg"); // 被压缩文件对象 File targetFile = new File("C:/图_111.jpg"); // 被压缩文件对象 // of(File f):表示压缩哪一个图片文件 // size(int width,int hight):压缩的图片最终的大小 // toFile(File f):最终的图片对象 Thumbnails.of(srcFile).size(100, 100).toFile(targetFile); }
/** * Chiama il super costruttore animaleView associando l'oggetto alla pecora presa da parametro. * Carica l'immagine della pecora. * * @param pecora pecora associata a questa view. */ public PecoraView(Pecora pecora) { super(pecora); try { immagine = ImageIO.read(getClass().getResourceAsStream(Costanti.PERCORSO_FILE_PECORA)); immagine = Thumbnails.of(immagine).scale(Costanti.FATTORE_SCALA).asBufferedImage(); } catch (IOException e) { new FinestraNotifica( "Errore nell'apertura dell'immagine associata all'oggetto \"pecora\"", e); } }
// 裁剪 private void test6() throws Exception { // sourceRegion() // 图片中心400*400的区域 Thumbnails.of(getImageStream()) .sourceRegion(Positions.CENTER, 400, 400) .size(200, 200) .keepAspectRatio(false) .toFile(ouputImagePath("a380_region_center.jpg")); // 图片右下400*400的区域 Thumbnails.of(getImageStream()) .sourceRegion(Positions.BOTTOM_RIGHT, 400, 400) .size(200, 200) .keepAspectRatio(false) .toFile(ouputImagePath("a380_region_bootom_right.jpg")); // 指定坐标 Thumbnails.of(getImageStream()) .sourceRegion(600, 500, 400, 400) .size(200, 200) .keepAspectRatio(false) .toFile(ouputImagePath("a380_region_coord.jpg")); }
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) { e.printStackTrace(); } }
private void writeImage(BufferedImage image, ImageType type, OutputStream output) throws IOException { double scaleFactor = type.getScaleFactor(image.getWidth(), image.getHeight()); if (retina) { scaleFactor = scaleFactor + scaleFactor; } BufferedImage scaledImage = null; if (scaleFactor < 1.0) { // scale down only scaledImage = Thumbnails.of(image).scale(scaleFactor).asBufferedImage(); } try { ImageIO.write(scaledImage != null ? scaledImage : image, type.getFileType(), output); } finally { if (scaledImage != null) { scaledImage.flush(); } } }
@RequestMapping(value = "addMember", method = RequestMethod.POST) public @ResponseBody int addMember(@RequestParam("files") MultipartFile image, Member member) throws Exception { System.out.println("/member/addMember"); int fileIndex = 0; int totalFile = 0; String genId = null; String extName = null; String filePath = servletCtx.getRealPath("/mem_upload") + "/"; String thumbPath = servletCtx.getRealPath("/mem_thumb") + "/"; genId = UUID.randomUUID().toString(); fileIndex = image.getOriginalFilename().lastIndexOf("."); extName = image.getOriginalFilename().substring(fileIndex, image.getOriginalFilename().length()); member.setOriImgName(image.getOriginalFilename()); member.setStoImgName(genId + extName); int row = memberService.jsonAddMember(member); if (row > 0) { try { FileCopyUtils.copy(image.getBytes(), new FileOutputStream(filePath + genId + extName)); Thumbnails.of(new File(filePath + genId + extName)) .size(216, 146) .toFiles(new File(thumbPath), Rename.NO_CHANGE); } catch (IOException e) { e.printStackTrace(); } } return row; }
public boolean getFile(FileDownloadTools fdt, String file, int width, int height) throws Exception { try { if (fdt.get(file)) { // 缩略图文件 File thumbFile = new File(fdt.getFilePath() + "_" + width + "_" + height + ".jpg"); try { // 如果缩略图不存在则生成 if (!thumbFile.exists()) { // 生成缩略图 Thumbnails.of(new File(fdt.getFilePath())) .size(width, height) .outputFormat("jpg") .toFile(thumbFile); } } catch (IOException e) { e.printStackTrace(); } fdt.setFilePath(thumbFile.getPath()); fdt.setFileName(width + "_" + height + "_" + fdt.getFileName()); getServletResponse().setHeader("charset", "ISO8859-1"); if (this.fileName == null) { this.fileName = fdt.getFileName(); } this.fileName = new String(this.fileName.getBytes(), "ISO8859-1"); // this.fileName = fdt.getFileName(); this.contentType = fdt.getContentType(); return true; } else { return false; } } catch (Exception e) { throw e; } }
public static void main(String[] args) throws Exception { Thumbnails.of(new File("WebContent/wx/images").listFiles()) .scale(0.25, 0.25) .outputFormat("jpg") .toFiles(MX_PREFIX_DOT_THUMBNAIL); }
public static BufferedImage scaleImage(BufferedImage bufferedImage, float scaleRatio) throws IOException { // apply scale return Thumbnails.of(bufferedImage).scale(scaleRatio).asBufferedImage(); }
// 按照比例进行缩放 private void test2() throws Exception { // scale(比例) Thumbnails.of("images/a380_1280x1024.jpg").scale(0.25f).toFile("img/a380_25%.jpg"); Thumbnails.of("images/a380_1280x1024.jpg").scale(1.10f).toFile("img/a380_110%.jpg"); }
// 输出到BufferedImage private void test9() throws Exception { // asBufferedImage() 返回BufferedImage BufferedImage thumbnail = Thumbnails.of("images/a380_1280x1024.jpg").size(1280, 1024).asBufferedImage(); ImageIO.write(thumbnail, "jpg", new File("img/a380_1280x1024_BufferedImage.jpg")); }
// 输出到OutputStream private void test8() throws Exception { // toOutputStream(流对象) OutputStream os = new FileOutputStream("img/a380_1280x1024_OutputStream.png"); Thumbnails.of("images/a380_1280x1024.jpg").size(1280, 1024).toOutputStream(os); }
@Transactional public static Result uploadPhoto() { final String loggedInUser = Application.getLoggedInUser(); if (loggedInUser == null) { return ok(views.html.login.render()); } DynamicForm form = DynamicForm.form().bindFromRequest(); boolean fullSize = Boolean.parseBoolean(form.get("fullSize")); boolean thumbnail = Boolean.parseBoolean(form.get("thumbnail")); boolean miniThumbnail = Boolean.parseBoolean(form.get("miniThumbnail")); boolean mobile = Boolean.parseBoolean(form.get("mobile")); String category = form.get("category"); ImageUploadUtil imageUploadUtil = new ImageUploadUtil(category); FilePart picture = request().body().asMultipartFormData().getFile("cover-photo"); String fileName = picture.getFilename(); DateTime now = new DateTime(); File file = picture.getFile(); String imageUrl; String imagePath; try { imageUrl = imageUploadUtil.getImageUrl(now, fileName); imagePath = imageUploadUtil.getImagePath(now, fileName); FileUtils.copyFile(file, new File(imagePath)); } catch (IOException e) { return status(500); } if (thumbnail == true) { StringBuffer sb = new StringBuffer(fileName); sb.insert(fileName.indexOf("."), "_thumb"); String name = sb.toString(); try { String imagePath2 = imageUploadUtil.getImagePath(now, name); BufferedImage originalImage = ImageIO.read(file); File file2 = new File(imagePath2); Thumbnails.of(originalImage).size(200, 200).toFile(file2); } catch (IOException e) { return status(500); } } if (miniThumbnail == true) { StringBuffer sb = new StringBuffer(fileName); sb.insert(fileName.indexOf("."), "_minithumb"); String name = sb.toString(); try { String imagePath2 = imageUploadUtil.getImagePath(now, name); BufferedImage originalImage = ImageIO.read(file); File file2 = new File(imagePath2); Thumbnails.of(originalImage).size(150, 150).toFile(file2); } catch (IOException e) { return status(500); } } if (mobile == true) { StringBuffer sb = new StringBuffer(fileName); sb.insert(fileName.indexOf("."), "_m"); String name = sb.toString(); try { String imagePath2 = imageUploadUtil.getImagePath(now, name); BufferedImage originalImage = ImageIO.read(file); File file2 = new File(imagePath2); Thumbnails.of(originalImage).size(100, 100).toFile(file2); } catch (IOException e) { return status(500); } if (thumbnail == true) { StringBuffer sb1 = new StringBuffer(fileName); sb1.insert(fileName.indexOf("."), "_thumb_m"); String name1 = sb1.toString(); try { String imagePath2 = imageUploadUtil.getImagePath(now, name1); BufferedImage originalImage = ImageIO.read(file); File file2 = new File(imagePath2); Thumbnails.of(originalImage).size(80, 80).toFile(file2); } catch (IOException e) { return status(500); } } if (miniThumbnail == true) { StringBuffer sb1 = new StringBuffer(fileName); sb1.insert(fileName.indexOf("."), "_minithumb_m"); String name1 = sb1.toString(); try { String imagePath2 = imageUploadUtil.getImagePath(now, name1); BufferedImage originalImage = ImageIO.read(file); File file2 = new File(imagePath2); Thumbnails.of(originalImage).size(50, 50).toFile(file2); } catch (IOException e) { return status(500); } } } Map<String, String> map = new HashMap<>(); map.put("URL", Application.APPLICATION_BASE_URL + imageUrl); return ok(Json.toJson(map)); }