Java Code Examples for org.imgscalr.Scalr#resize()
The following examples show how to use
org.imgscalr.Scalr#resize() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProfileUtils.java From sakai with Educational Community License v2.0 | 6 votes |
/** * Scale an image so it is fit within a give width and height, whilst maintaining its original proportions * * @param imageData bytes of the original image * @param maxSize maximum dimension in px */ public static byte[] scaleImage(InputStream in, int maxSize, String mimeType) { byte[] scaledImageBytes = null; try { //convert original image to inputstream //original buffered image BufferedImage originalImage = ImageIO.read(in); //scale the image using the imgscalr library BufferedImage scaledImage = Scalr.resize(originalImage, maxSize); //convert BufferedImage to byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(scaledImage, getInformalFormatForMimeType(mimeType), baos); baos.flush(); scaledImageBytes = baos.toByteArray(); baos.close(); } catch (Exception e) { log.error("Scaling image failed.", e); } return scaledImageBytes; }
Example 2
Source File: ImageUtil.java From Knowage-Server with GNU Affero General Public License v3.0 | 6 votes |
synchronized public static void resize(String filename, InputStream orig, OutputStream dest, int width, int height) throws IOException { if (width > 0 || height > 0) { String formatName = getFormatName(filename); Mode mode = Mode.AUTOMATIC; if (height == 0) mode = Mode.FIT_TO_WIDTH; else if (width == 0) mode = Mode.FIT_TO_HEIGHT; BufferedImage src = ImageIO.read(orig); BufferedImage thumbnail = null; if (src.getHeight() < height && src.getWidth() < width) { thumbnail = src; } else { thumbnail = Scalr.resize(src, Method.ULTRA_QUALITY, mode, width, height); } if (!ImageIO.write(thumbnail, formatName, dest)) { throw new IOException("ImageIO.write error"); } } }
Example 3
Source File: ReportContext.java From carina with Apache License 2.0 | 6 votes |
@Override public void run() { try { if (width > 0 && height > 0) { BufferedImage resizedImage = Scalr.resize(image, Scalr.Method.BALANCED, Scalr.Mode.FIT_TO_WIDTH, width, height, Scalr.OP_ANTIALIAS); if (resizedImage.getHeight() > height) { resizedImage = Scalr.crop(resizedImage, resizedImage.getWidth(), height); } ImageIO.write(resizedImage, "PNG", new File(path)); } else { ImageIO.write(image, "PNG", new File(path)); } } catch (Exception e) { LOGGER.error("Unable to save screenshot: " + e.getMessage()); } }
Example 4
Source File: PdfPanel.java From swift-explorer with Apache License 2.0 | 6 votes |
private BufferedImage getBestFit (BufferedImage bi, int maxWidth, int maxHeight) { if (bi == null) return null ; Mode mode = Mode.AUTOMATIC ; int maxSize = Math.min(maxWidth, maxHeight) ; double dh = (double)bi.getHeight() ; if (dh > Double.MIN_VALUE) { double imageAspectRatio = (double)bi.getWidth() / dh ; if (maxHeight * imageAspectRatio <= maxWidth) { maxSize = maxHeight ; mode = Mode.FIT_TO_HEIGHT ; } else { maxSize = maxWidth ; mode = Mode.FIT_TO_WIDTH ; } } return Scalr.resize(bi, Method.QUALITY, mode, maxSize, Scalr.OP_ANTIALIAS) ; }
Example 5
Source File: ImagePanel.java From swift-explorer with Apache License 2.0 | 5 votes |
/** * {@inheritDoc}. */ @Override protected synchronized void paintComponent(Graphics g) { g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, getWidth(), getHeight()); if (image != null) { Mode mode = Mode.AUTOMATIC ; int maxSize = Math.min(this.getWidth(), this.getHeight()) ; double dh = (double)image.getHeight() ; if (dh > Double.MIN_VALUE) { double imageAspectRatio = (double)image.getWidth() / dh ; if (this.getHeight() * imageAspectRatio <= this.getWidth()) { maxSize = this.getHeight() ; mode = Mode.FIT_TO_HEIGHT ; } else { maxSize = this.getWidth() ; mode = Mode.FIT_TO_WIDTH ; } } BufferedImage scaledImg = Scalr.resize(image, Method.AUTOMATIC, mode, maxSize, Scalr.OP_ANTIALIAS) ; g.drawImage(scaledImg, 0, 0, scaledImg.getWidth(), scaledImg.getHeight(), this); } }
Example 6
Source File: ActionSetPhoto.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
ActionResult<Wo> execute(EffectivePerson effectivePerson, String id, byte[] bytes, FormDataContentDisposition disposition) throws Exception { try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { ActionResult<Wo> result = new ActionResult<>(); Business business = new Business(emc); Room room = emc.find(id, Room.class); if (null == room) { throw new ExceptionRoomNotExist(id); } if (!business.roomEditAvailable(effectivePerson, room)) { throw new ExceptionRoomAccessDenied(effectivePerson, room.getName()); } try (InputStream input = new ByteArrayInputStream(bytes); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { BufferedImage image = ImageIO.read(input); BufferedImage scalrImage = Scalr.resize(image, 512, 512); ImageIO.write(scalrImage, "png", baos); emc.beginTransaction(Room.class); String str = Base64.encodeBase64String(baos.toByteArray()); room.setPhoto(str); emc.commit(); Wo wo = new Wo(); wo.setValue(true); result.setData(wo); } return result; } }
Example 7
Source File: ImageUtil.java From mycollab with GNU Affero General Public License v3.0 | 5 votes |
/** * @param buffImage * @param percenScale * @return */ public static BufferedImage scaleImage(BufferedImage buffImage, float percenScale) { float width = buffImage.getWidth() * percenScale; float height = buffImage.getHeight() * percenScale; return Scalr.resize(buffImage, Method.BALANCED, Mode.AUTOMATIC, (int) width, (int) height); }
Example 8
Source File: ActionSetIcon.java From o2oa with GNU Affero General Public License v3.0 | 5 votes |
ActionResult<Wo> execute(EffectivePerson effectivePerson, String id, byte[] bytes, FormDataContentDisposition disposition) throws Exception { try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { ActionResult<Wo> result = new ActionResult<>(); Business business = new Business(emc); Application application = emc.find(id, Application.class); if (null == application) { throw new ExceptionApplicationNotExist(id); } if (!business.editable(effectivePerson, application)) { throw new ExceptionApplicationAccessDenied(effectivePerson.getDistinguishedName(), application.getName(), application.getId()); } try (ByteArrayInputStream bais = new ByteArrayInputStream(bytes); ByteArrayOutputStream baos = new ByteArrayOutputStream()) { BufferedImage image = ImageIO.read(bais); BufferedImage scalrImage = Scalr.resize(image, 72, 72); ImageIO.write(scalrImage, "png", baos); String icon = Base64.encodeBase64String(baos.toByteArray()); String iconHue = ImageTools.hue(scalrImage); emc.beginTransaction(Application.class); application.setIcon(icon); application.setIconHue(iconHue); emc.commit(); ApplicationCache.notify(Application.class); Wo wo = new Wo(); wo.setId(application.getId()); result.setData(wo); } return result; } }
Example 9
Source File: ComponentsFactory.java From MercuryTrade with MIT License | 5 votes |
public ImageIcon getIcon(String iconPath, float size) { BufferedImage icon = null; try { BufferedImage buttonIcon = ImageIO.read(getClass().getClassLoader().getResource(iconPath)); icon = Scalr.resize(buttonIcon, (int) (scale * size)); } catch (IOException e) { log.error(e); } return new ImageIcon(icon); }
Example 10
Source File: ImgscalrImageScaler.java From scipio-erp with Apache License 2.0 | 5 votes |
@Override protected BufferedImage scaleImageCore(BufferedImage image, int targetWidth, int targetHeight, Map<String, Object> options) throws IOException { // FIXME?: imgscalr supports no target image types at all... BufferedImage result = Scalr.resize(image, getFilter(options), Scalr.Mode.FIT_EXACT, targetWidth, targetHeight); ImageType targetType = getMergedTargetImageType(options, ImageType.EMPTY); ImageTypeInfo targetTypeInfo = targetType.getImageTypeInfoFor(image); // FIXME?: for now don't bother post-converting anything at all unless we're forced... return isPostConvertResultImage(image, options, targetTypeInfo) ? checkConvertResultImageType(image, result, options, targetTypeInfo) : result; }
Example 11
Source File: ProgressiveAlgorithm.java From density-converter with Apache License 2.0 | 5 votes |
@Override public BufferedImage scale(BufferedImage imageToScale, int dWidth, int dHeight) { switch (type) { case NOBEL_BILINEAR: return new MultiStepRescaleOp(dWidth, dHeight, RenderingHints.VALUE_INTERPOLATION_BILINEAR) .filter(imageToScale, null); case NOBEL_BICUBUC: return new MultiStepRescaleOp(dWidth, dHeight, RenderingHints.VALUE_INTERPOLATION_BICUBIC) .filter(imageToScale, null); case NOBEL_NEAREST_NEIGHBOR: return new MultiStepRescaleOp(dWidth, dHeight, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) .filter(imageToScale, null); case NOBEL_LANCZOS3: return new MultiStepLanczos3RescaleOp(dWidth, dHeight).filter(imageToScale, null); case PROGRESSIVE_BILINEAR_AND_LANCZOS2: return scaleProgressiveLanczos(imageToScale, dWidth, dHeight, 2); case PROGRESSIVE_BILINEAR_AND_LANCZOS3: return scaleProgressiveLanczos(imageToScale, dWidth, dHeight, 3); case THUMBNAILATOR_BILINEAR: return new ThumbnailnatorProgressiveAlgorithm(RenderingHints.VALUE_INTERPOLATION_BILINEAR).scale(imageToScale, dWidth, dHeight); case THUMBNAILATOR_BICUBUC: return new ThumbnailnatorProgressiveAlgorithm(RenderingHints.VALUE_INTERPOLATION_BICUBIC).scale(imageToScale, dWidth, dHeight); case IMGSCALR_SEVENTH_STEP: return Scalr.resize(imageToScale, Scalr.Method.ULTRA_QUALITY, Scalr.Mode.FIT_EXACT, dWidth, dHeight, null); case IMGSCALR_HALF_STEP: return Scalr.resize(imageToScale, Scalr.Method.QUALITY, Scalr.Mode.FIT_EXACT, dWidth, dHeight, null); default: throw new IllegalArgumentException("unknown algorithm"); } }
Example 12
Source File: ImageProcessor.java From spring-batch-performance-tuning with Apache License 2.0 | 5 votes |
@Override public ProcessedImage process(ImageSubmission imageSubmission) throws Exception { final String imagePath = path + imageSubmission.getFileName(); try { final BufferedImage originalImage = ImageIO.read(new File(imagePath)); final BufferedImage resizedImage = Scalr.resize(originalImage, width, height); return new ProcessedImage(resizedImage, imageSubmission.getFileName()); } catch (Exception e) { LOG.warn("Failed to convert " + imagePath + ", skipping - sorry submitter! (" + e.getMessage() + ")"); return null; } }
Example 13
Source File: ImageProcessor.java From spring-batch-performance-tuning with Apache License 2.0 | 5 votes |
@Override public ProcessedImage process(ImageSubmission imageSubmission) throws Exception { final String imagePath = path + imageSubmission.getFileName(); try { final BufferedImage originalImage = ImageIO.read(new File(imagePath)); final BufferedImage resizedImage = Scalr.resize(originalImage, width, height); return new ProcessedImage(resizedImage, imageSubmission.getFileName()); } catch (Exception e) { LOG.warn("Failed to convert " + imagePath + ", skipping - sorry submitter! (" + e.getMessage() + ")"); return null; } }
Example 14
Source File: ActionCopy.java From o2oa with GNU Affero General Public License v3.0 | 4 votes |
private String copy(EffectivePerson effectivePerson, Business business, ReferenceType referenceType, String reference, Attachment attachment, Integer scale) throws Exception { StorageMapping attachmentMapping = ThisApplication.context().storageMappings().get(Attachment.class, attachment.getStorage()); if (null == attachmentMapping) { throw new ExceptionStorageMappingNotExisted(attachment.getStorage()); } StorageMapping fileMapping = ThisApplication.context().storageMappings().random(File.class); if (null == fileMapping) { throw new ExceptionAllocateStorageMaaping(); } /** 由于这里需要根据craeteTime创建path,先进行赋值,再进行校验,最后保存 */ /** 禁止不带扩展名的文件上传 */ if (StringUtils.isEmpty(FilenameUtils.getExtension(attachment.getName()))) { throw new ExceptionEmptyExtension(attachment.getName()); } File file = new File(fileMapping.getName(), attachment.getName(), effectivePerson.getDistinguishedName(), referenceType, reference); business.entityManagerContainer().check(file, CheckPersistType.all); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { attachment.readContent(attachmentMapping, baos); try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { if ((scale > 0) && ArrayUtils.contains(IMAGE_EXTENSIONS, file.getExtension())) { /** 进行图形缩放 */ BufferedImage image = ImageIO.read(bais); BufferedImage scalrImage = Scalr.resize(image, scale); try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { ImageIO.write(scalrImage, file.getExtension(), out); try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) { file.saveContent(fileMapping, in, FilenameUtils.getName(attachment.getName())); } } } else { file.saveContent(fileMapping, bais, FilenameUtils.getName(attachment.getName())); } } } business.entityManagerContainer().beginTransaction(File.class); business.entityManagerContainer().persist(file); business.entityManagerContainer().commit(); return file.getId(); }
Example 15
Source File: ImageUtils.java From RemoteSupportTool with Apache License 2.0 | 4 votes |
public static BufferedImage resize(BufferedImage image, int maxWidth, int maxHeight) { return Scalr.resize(image, Scalr.Method.BALANCED, Scalr.Mode.BEST_FIT_BOTH, maxWidth, maxHeight); }
Example 16
Source File: ActionUploadOctetStream.java From o2oa with GNU Affero General Public License v3.0 | 4 votes |
ActionResult<Wo> execute(EffectivePerson effectivePerson, String referenceType, String reference, Integer scale, byte[] bytes) throws Exception { try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create(); ByteArrayInputStream in = new ByteArrayInputStream(bytes)) { ActionResult<Wo> result = new ActionResult<>(); ReferenceType type = EnumUtils.getEnum(ReferenceType.class, referenceType); StorageMapping mapping = ThisApplication.context().storageMappings().random(File.class); if (null == mapping) { throw new ExceptionAllocateStorageMaaping(); } String fileName = "image.jpg"; File file = new File(mapping.getName(), fileName, effectivePerson.getDistinguishedName(), type, reference); emc.check(file, CheckPersistType.all); if ((scale > 0) && ArrayUtils.contains(IMAGE_EXTENSIONS, file.getExtension())) { /** 如果是需要压缩的附件 */ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { BufferedImage image = ImageIO.read(in); if (image.getWidth() > scale) { /** 图像的实际大小比scale大的要进行压缩 */ BufferedImage scalrImage = Scalr.resize(image, Method.QUALITY, Mode.FIT_TO_WIDTH, scale); ImageIO.write(scalrImage, file.getExtension(), baos); } else { /** 图像的实际大小比scale小,保存原图不进行压缩 */ ImageIO.write(image, file.getExtension(), baos); } try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { file.saveContent(mapping, bais, fileName); } } } else { file.saveContent(mapping, in, fileName); } emc.beginTransaction(File.class); emc.persist(file); emc.commit(); ApplicationCache.notify(File.class); Wo wo = new Wo(); wo.setId(file.getId()); result.setData(wo); return result; } }
Example 17
Source File: ActionDownloadImageWidthHeight.java From o2oa with GNU Affero General Public License v3.0 | 4 votes |
ActionResult<Wo> execute(EffectivePerson effectivePerson, String id, Integer width, Integer height) throws Exception { try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) { ActionResult<Wo> result = new ActionResult<>(); Attachment2 attachment = emc.find(id, Attachment2.class, ExceptionWhen.not_found); /* 判断文件的当前用户是否是管理员或者文件创建者 或者当前用户在分享或者共同编辑中 */ if (effectivePerson.isNotManager() && effectivePerson.isNotPerson(attachment.getPerson())) { throw new Exception("person{name:" + effectivePerson.getDistinguishedName() + "} access attachment{id:" + id + "} denied."); } if (!ArrayUtils.contains(IMAGE_EXTENSIONS, attachment.getExtension())) { throw new Exception("attachment not image file."); } if (width < 0 || width > 5000) { throw new Exception("invalid width:" + width + "."); } if (height < 0 || height > 5000) { throw new Exception("invalid height:" + height + "."); } OriginFile originFile = emc.find(attachment.getOriginFile(),OriginFile.class); if (null == originFile) { throw new ExceptionAttachmentNotExist(id,attachment.getOriginFile()); } Wo wo = null; String cacheKey = ApplicationCache.concreteCacheKey(this.getClass(), id+width+height); Element element = cache.get(cacheKey); if ((null != element) && (null != element.getObjectValue())) { wo = (Wo) element.getObjectValue(); result.setData(wo); } else { StorageMapping mapping = ThisApplication.context().storageMappings().get(OriginFile.class, originFile.getStorage()); try (ByteArrayOutputStream output = new ByteArrayOutputStream()) { originFile.readContent(mapping, output); try (ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray())) { BufferedImage src = ImageIO.read(input); int scalrWidth = (width == 0) ? src.getWidth() : width; int scalrHeight = (height == 0) ? src.getHeight() : height; Scalr.Mode mode = Scalr.Mode.FIT_TO_WIDTH; if(src.getWidth()>src.getHeight()){ mode = Scalr.Mode.FIT_TO_HEIGHT; } BufferedImage scalrImage = Scalr.resize(src,Scalr.Method.SPEED, mode, NumberUtils.min(scalrWidth, src.getWidth()), NumberUtils.min(scalrHeight, src.getHeight())); try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { ImageIO.write(scalrImage, "png", baos); byte[] bs = baos.toByteArray(); wo = new Wo(bs, this.contentType(false, attachment.getName()), this.contentDisposition(false, attachment.getName())); cache.put(new Element(cacheKey, wo)); result.setData(wo); } } } } return result; } }
Example 18
Source File: ActionUploadOctetStream.java From o2oa with GNU Affero General Public License v3.0 | 4 votes |
ActionResult<Wo> execute(EffectivePerson effectivePerson, String referenceType, String reference, Integer scale, byte[] bytes) throws Exception { try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create(); ByteArrayInputStream in = new ByteArrayInputStream(bytes)) { ActionResult<Wo> result = new ActionResult<>(); ReferenceType type = EnumUtils.getEnum(ReferenceType.class, referenceType); StorageMapping mapping = ThisApplication.context().storageMappings().random(File.class); if (null == mapping) { throw new ExceptionAllocateStorageMaaping(); } String fileName = "image.jpg"; File file = new File(mapping.getName(), fileName, effectivePerson.getDistinguishedName(), type, reference); emc.check(file, CheckPersistType.all); if ((scale > 0) && ArrayUtils.contains(IMAGE_EXTENSIONS, file.getExtension())) { /** 如果是需要压缩的附件 */ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { BufferedImage image = ImageIO.read(in); if (image.getWidth() > scale) { /** 图像的实际大小比scale大的要进行压缩 */ BufferedImage scalrImage = Scalr.resize(image, Method.QUALITY, Mode.FIT_TO_WIDTH, scale); ImageIO.write(scalrImage, file.getExtension(), baos); } else { /** 图像的实际大小比scale小,保存原图不进行压缩 */ ImageIO.write(image, file.getExtension(), baos); } try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { file.saveContent(mapping, bais, fileName); } } } else { file.saveContent(mapping, in, fileName); } emc.beginTransaction(File.class); emc.persist(file); emc.commit(); ApplicationCache.notify(File.class); Wo wo = new Wo(); wo.setId(file.getId()); result.setData(wo); return result; } }
Example 19
Source File: ActionUpload.java From o2oa with GNU Affero General Public License v3.0 | 4 votes |
ActionResult<Wo> execute(EffectivePerson effectivePerson, String referenceType, String reference, Integer scale, byte[] bytes, FormDataContentDisposition disposition) throws Exception { try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create(); ByteArrayInputStream in = new ByteArrayInputStream(bytes)) { ActionResult<Wo> result = new ActionResult<>(); ReferenceType type = EnumUtils.getEnum(ReferenceType.class, referenceType); StorageMapping mapping = ThisApplication.context().storageMappings().random(File.class); if (null == mapping) { throw new ExceptionAllocateStorageMaaping(); } /** 由于这里需要根据craeteTime创建path,先进行赋值,再进行校验,最后保存 */ /** 禁止不带扩展名的文件上传 */ /** 文件名编码转换 */ String fileName = new String(disposition.getFileName().getBytes(DefaultCharset.charset_iso_8859_1), DefaultCharset.charset); fileName = FilenameUtils.getName(fileName); if (StringUtils.isEmpty(FilenameUtils.getExtension(fileName))) { throw new ExceptionEmptyExtension(fileName); } File file = new File(mapping.getName(), fileName, effectivePerson.getDistinguishedName(), type, reference); emc.check(file, CheckPersistType.all); if ((scale > 0) && ArrayUtils.contains(IMAGE_EXTENSIONS, file.getExtension())) { /** 如果是需要压缩的附件 */ try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { BufferedImage image = ImageIO.read(in); if (image.getWidth() > scale) { /** 图像的实际大小比scale大的要进行压缩 */ BufferedImage scalrImage = Scalr.resize(image, Method.QUALITY, Mode.FIT_TO_WIDTH, scale); ImageIO.write(scalrImage, file.getExtension(), baos); } else { /** 图像的实际大小比scale小,保存原图不进行压缩 */ ImageIO.write(image, file.getExtension(), baos); } try (ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray())) { file.saveContent(mapping, bais, fileName); } } } else { file.saveContent(mapping, in, fileName); } emc.beginTransaction(File.class); emc.persist(file); emc.commit(); ApplicationCache.notify(File.class); Wo wo = new Wo(); wo.setId(file.getId()); result.setData(wo); return result; } }
Example 20
Source File: DefaultImageProcessingService.java From dhis2-core with BSD 3-Clause "New" or "Revised" License | 4 votes |
private BufferedImage resize( BufferedImage image, ImageSize dimensions ) { return Scalr.resize( image, Scalr.Method.BALANCED, Scalr.Mode.FIT_TO_WIDTH, dimensions.width, dimensions.height ); }