List of usage examples for java.awt.image BufferedImage getScaledInstance
public Image getScaledInstance(int width, int height, int hints)
From source file:org.springframework.boot.ImageBanner.java
private BufferedImage resizeImage(BufferedImage image, int width, int height) { if (width < 1) { width = 1;/*from w ww . j a va 2 s. c o m*/ } if (height <= 0) { double aspectRatio = (double) width / image.getWidth() * 0.5; height = (int) Math.ceil(image.getHeight() * aspectRatio); } BufferedImage resized = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Image scaled = image.getScaledInstance(width, height, Image.SCALE_DEFAULT); resized.getGraphics().drawImage(scaled, 0, 0, null); return resized; }
From source file:view.ManageOrganization.java
private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed // TODO add your handling code here: JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & GIF Images", "jpg", "gif"); chooser.setFileFilter(filter);//from w w w. j av a 2s .c om int returnVal = chooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { chooser.getSelectedFile().getPath(); System.out.println("You chose to open this file: " + chooser.getSelectedFile().getName()); BufferedImage img = null; try { img = ImageIO.read(new File(chooser.getSelectedFile().getPath())); Image scaledInstance = img.getScaledInstance(organisation_logo_label.getWidth(), organisation_logo_label.getHeight(), Image.SCALE_SMOOTH); ImageIcon imageIcon = new ImageIcon(scaledInstance); organisation_logo_label.setIcon(imageIcon); organisationIconName = System.currentTimeMillis() + "." + FilenameUtils.getExtension(chooser.getSelectedFile().getPath()); FileUtils.copyFile(chooser.getSelectedFile(), new File(Configuration.organisationImages + organisationIconName)); } catch (IOException e) { e.printStackTrace(); } long size = (chooser.getSelectedFile().length()) / 1024; } }
From source file:jp.canetrash.maven.plugin.bijint.BujintMojo.java
/** * ??????????//from w w w. j av a2 s . c om * * @param url * @return * @throws Exception */ BufferedImage getFittingImage(String url) throws Exception { // ?? DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = buildDefaultHttpMessage(new HttpGet(url)); httpget.setHeader("Referer", "http://www.bijint.com/jp/"); HttpResponse response = httpclient.execute(httpget); BufferedImage image = ImageIO.read(response.getEntity().getContent()); httpclient.getConnectionManager().shutdown(); int width = image.getWidth() / 10 * 4; int height = image.getHeight() / 10 * 4; BufferedImage resizedImage = null; resizedImage = new BufferedImage(width, height, image.getType()); resizedImage.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING), 0, 0, width, height, null); return resizedImage; }
From source file:net.pkhsolutions.pecsapp.control.PictureTransformer.java
/** * TODO Document me//from www. j a v a2 s. c o m * * @param image * @param maxHeightInPx * @param maxWidthInPx * @return */ @NotNull public BufferedImage scaleIfNecessary(@NotNull BufferedImage image, int maxHeightInPx, int maxWidthInPx) { int w = image.getWidth(); int h = image.getHeight(); int newW; int newH; if (w <= maxWidthInPx && h <= maxHeightInPx) { return image; } else if (w > h) { newW = maxWidthInPx; newH = newW * h / w; } else { newH = maxHeightInPx; newW = newH * w / h; } LOGGER.info("Original size was w{} x h{}, new size is w{} x h{}", w, h, newW, newH); Image tmp = image.getScaledInstance(newW, newH, Image.SCALE_SMOOTH); BufferedImage img = new BufferedImage(newW, newH, image.getType()); Graphics2D g2d = img.createGraphics(); g2d.drawImage(tmp, 0, 0, null); g2d.dispose(); return img; }
From source file:org.yes.cart.service.domain.impl.ImageServiceImpl.java
/** * {@inheritDoc}//from w w w . j a va 2s .co m */ public byte[] resizeImage(final String filename, final byte[] content, final String width, final String height, final boolean cropToFit) { try { final InputStream bis = new ByteArrayInputStream(content); final BufferedImage originalImg = ImageIO.read(bis); final String codec = getCodecFromFilename(filename); final boolean supportsAlpha = hasAlphaSupport(codec); int x = NumberUtils.toInt(width); int y = NumberUtils.toInt(height); int originalX = originalImg.getWidth(); int originalY = originalImg.getHeight(); boolean doCropToFit = cropToFit || x < forceCropToFitOnSize || y < forceCropToFitOnSize; final int imageType = originalImg.getType(); final Image resizedImg; // final BufferedImage resizedImg; final int padX, padY; if (doCropToFit) { // crop the original to best fit of target size int[] cropDims = cropImageToCenter(x, y, originalX, originalY); padX = 0; padY = 0; final BufferedImage croppedImg = originalImg.getSubimage(cropDims[0], cropDims[1], cropDims[2], cropDims[3]); resizedImg = croppedImg.getScaledInstance(x, y, Image.SCALE_SMOOTH); // final BufferedImage croppedImg = originalImg.getSubimage(cropDims[0], cropDims[1], cropDims[2], cropDims[3]); // resizedImg = new BufferedImage(y, x, imageType); // Graphics2D graphics = resizedImg.createGraphics(); // graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // graphics.drawImage(croppedImg, 0, 0, x, y, null); } else { int[] scaleDims = scaleImageToCenter(x, y, originalX, originalY); padX = scaleDims[0]; padY = scaleDims[1]; resizedImg = originalImg.getScaledInstance(scaleDims[2], scaleDims[3], Image.SCALE_SMOOTH); // resizedImg = new BufferedImage(scaleDims[3], scaleDims[2], imageType); // Graphics2D graphics = resizedImg.createGraphics(); // graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); // graphics.drawImage(originalImg, 0, 0, scaleDims[2], scaleDims[3], null); } // base canvas final BufferedImage resizedImgFinal = new BufferedImage(x, y, imageType); // fill base color final Graphics2D graphics = resizedImgFinal.createGraphics(); graphics.setPaint(supportsAlpha ? alphaBorder : defaultBorder); graphics.fillRect(0, 0, x, y); // insert scaled image graphics.drawImage(resizedImg, padX, padY, null); final ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(resizedImgFinal, codec, bos); return bos.toByteArray(); } catch (Exception exp) { ShopCodeContext.getLog(this).error("Unable to resize image " + filename, exp); } return new byte[0]; }
From source file:de.cismet.cids.custom.objecteditors.wunda_blau.WebDavPicturePanel.java
/** * DOCUMENT ME!// ww w . j a v a 2 s . c o m * * @param bi DOCUMENT ME! * @param component DOCUMENT ME! * @param insetX DOCUMENT ME! * @param insetY DOCUMENT ME! * * @return DOCUMENT ME! */ public static Image adjustScale(final BufferedImage bi, final JComponent component, final int insetX, final int insetY) { final double scalex = (double) component.getWidth() / bi.getWidth(); final double scaley = (double) component.getHeight() / bi.getHeight(); final double scale = Math.min(scalex, scaley); if (scale <= 1d) { return bi.getScaledInstance((int) (bi.getWidth() * scale) - insetX, (int) (bi.getHeight() * scale) - insetY, Image.SCALE_SMOOTH); } else { return bi; } }
From source file:de.anycook.upload.UploadHandler.java
/** * speichert eine grosse Version des Bildes * * @param image BufferedImage//from ww w.ja v a2 s. co m * @param filename Name der zu erzeugenden Datei */ private void saveBigImage(BufferedImage image, String filename) throws IOException { int height = image.getHeight(); int width = image.getWidth(); int newWidth = bigWidth; int newHeight = new Double(((double) height / (double) width) * newWidth).intValue(); if (bigHeight > -1 && newHeight > bigHeight) { newHeight = bigHeight; newWidth = new Double(((double) width / (double) height) * newHeight).intValue(); } BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB); newImage.getGraphics().drawImage(image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0, 0, null); imageSaver.save("big/", filename, newImage); }
From source file:View.Main.java
private void setImage(String path) throws IOException { BufferedImage bi = ImageIO.read(new File(path)); Image scaledImg = bi.getScaledInstance(jLabel_displayImage.getWidth(), jLabel_displayImage.getHeight(), Image.SCALE_DEFAULT); jLabel_displayImage.setIcon(new ImageIcon(scaledImg)); jLabel_displayImage.setHorizontalAlignment(JLabel.CENTER); jLabel_displayImage.setVerticalAlignment(JLabel.CENTER); }
From source file:org.fao.geonet.services.thumbnail.Set.java
private void createThumbnail(String inFile, String outFile, int scalingFactor, String scalingDir) throws IOException { BufferedImage origImg = getImage(inFile); int imgWidth = origImg.getWidth(); int imgHeight = origImg.getHeight(); int width;//from w w w .ja v a 2 s .c om int height; if (scalingDir.equals("width")) { width = scalingFactor; height = width * imgHeight / imgWidth; } else { height = scalingFactor; width = height * imgWidth / imgHeight; } Image thumb = origImg.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH); BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TRANSLUCENT); Graphics2D g = bimg.createGraphics(); g.drawImage(thumb, 0, 0, null); g.dispose(); ImageIO.write(bimg, IMAGE_TYPE, new File(outFile)); }
From source file:com.ace.erp.controller.sys.company.OrganizationController.java
@RequestMapping(value = "/company/uploadLogo", method = RequestMethod.POST) @ResponseBody// w ww .j ava2s.co m public Response uploadLogo(@CurrentUser User user, String srcImageFile, int x, int y, int destWidth, int destHeight, int srcShowWidth, int srcShowHeight, HttpServletRequest request) { try { String path = request.getSession().getServletContext().getRealPath("/"); String contextPath = request.getContextPath(); Image img; ImageFilter cropFilter; //String srcFileName = FilenameUtils.getName(srcImageFile); String srcFileName = StringUtils.isNotBlank(contextPath) ? srcImageFile.replaceFirst(contextPath, "") : srcImageFile; // ??? File srcFile = new File(path + "/" + srcFileName); BufferedImage bi = ImageIO.read(srcFile); //?????????? int srcWidth = bi.getWidth(); // ? int srcHeight = bi.getHeight(); // ? if (srcShowWidth == 0) srcShowWidth = srcWidth; if (srcShowHeight == 0) srcShowHeight = srcHeight; if (srcShowWidth >= destWidth && srcShowHeight >= destHeight) { //??? Image image = bi.getScaledInstance(srcShowWidth, srcShowHeight, Image.SCALE_DEFAULT); cropFilter = new CropImageFilter(x, y, destWidth, destHeight); img = Toolkit.getDefaultToolkit() .createImage(new FilteredImageSource(image.getSource(), cropFilter)); BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB); Graphics g = tag.getGraphics(); g.drawImage(img, 0, 0, null); // ?? g.dispose(); String ext = FilenameUtils.getExtension(srcImageFile); //path += HEADER_PIC; //User loginUser = SystemUtil.getLoginUser(request.getSession()); //String fileName = user.getOrganizationList().get(0).getId()+""; File destImageFile = new File(path + "/" + System.currentTimeMillis() + ".jpg"); // ImageIO.write(tag, ext, destImageFile); //loginUser.setPicPath(SystemConst.SYSTEM_CONTEXT_PATH_VALUE + HEADER_PIC + "/" + fileName); //userService.update(loginUser); // srcFile.delete(); return new Response(new ResponseHeader(200, 20)); } } catch (Exception e) { e.printStackTrace(); } return null; }