Java Code Examples for java.awt.image.BufferedImage.getScaledInstance()

The following are Jave code examples for showing how to use getScaledInstance() of the java.awt.image.BufferedImage class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: PiCameraProject   File: ErikDetectorAlgorithm.java   View Source Code Vote up 6 votes
@Override
public BufferedImage prepareImage(BufferedImage bi) {
    System.out.println("preparing " + bi + " at " + bi.getWidth() + "x" + bi.getHeight());
    final double resizePct = SCALE_WIDTH / (double)bi.getWidth();
    final int scaleHeight = (int) (resizePct * bi.getHeight());
    System.out.println("  scaling at " + resizePct + " is " + scaleHeight);
    final Image scaled = bi.getScaledInstance(SCALE_WIDTH, scaleHeight, Image.SCALE_SMOOTH);
    BufferedImage dimg = new BufferedImage(SCALE_WIDTH, scaleHeight, BufferedImage.TYPE_3BYTE_BGR);
    dimg.getGraphics().drawImage(scaled, 0, 0, null);
    for (int i = 0; i < scaleHeight; i++) {

        for (int j = 0; j < SCALE_WIDTH; j++) {

            Color c = new Color(dimg.getRGB(j, i));
            int red = (int) (c.getRed() * 0.299);
            int green = (int) (c.getGreen() * 0.587);
            int blue = (int) (c.getBlue() * 0.114);
            Color newColor = new Color(red + green + blue,
                    red + green + blue, red + green + blue);

            dimg.setRGB(j, i, newColor.getRGB());
        }
    }
    return dimg;
}
 
Example 2
Project: jmt   File: SampleQNAnimation.java   View Source Code Vote up 6 votes
private void createOverlayImage() {
	int height = getBounds().height * 2, width = getBounds().width * 2, qLength = width - (height * 4) / 5, qHeight = (height * 4) / 5, yOffs = (height - qHeight) / 2;
	overlayedShading = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
	Color[] shades = new Color[7], lights = new Color[7];
	for (int i = 0; i < shades.length; i++) {
		shades[i] = new Color(0, 0, 0, 160 - (i * 160) / shades.length);
		lights[i] = new Color(255, 255, 255, 200 - (i * 200) / lights.length);
	}
	Graphics g = overlayedShading.getGraphics();
	for (int i = 0; i < shades.length; i++) {
		g.setColor(lights[i]);
		g.drawPolyline(new int[] { i, i, qLength - i }, new int[] { height - i - yOffs, i + yOffs, i + yOffs }, 3);
		g.drawArc(width - height + i, i, height - 2 * i, height - 2 * i, 45 + 4 * i, 180 - 8 * i);
		g.setColor(shades[i]);
		g.drawPolyline(new int[] { i, qLength - i, qLength - i }, new int[] { height - i - yOffs, height - i - yOffs, i + yOffs }, 2);
		g.drawArc(width - height + i, i, height - 2 * i, height - 2 * i, 225 + 4 * i, 180 - 8 * i);
	}
	overlayedShading = overlayedShading.getScaledInstance(getBounds().width, getBounds().height, Image.SCALE_SMOOTH);
}
 
Example 3
Project: lemon-framework   File: ImageUtil.java   View Source Code Vote up 6 votes
/**
 * 按比例缩放
 * @param img
 * @return
 */
public static BufferedImage getScaleZoom(BufferedImage img, int targetSize) {
	int srcWidth = img.getWidth();
	int srcHeight = img.getHeight();
	int targetWidth = targetSize;
	int targetHeight = targetSize;
	if (targetSize>srcWidth || targetSize>srcHeight){
		targetWidth = srcWidth;
		targetHeight = srcHeight;
	}
	double rate1 = ((double)srcWidth)/targetWidth;
	double rate2 = ((double)srcHeight)/targetHeight;
       // 根据缩放比率大的进行缩放控制 
       double rate = rate1 > rate2 ? rate1 : rate2;
       int newWidth = (int)(((double)srcWidth)/rate);
       int newHeight = (int)(((double)srcHeight)/rate);
	int imageType = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
	BufferedImage tag = new BufferedImage(newWidth, newHeight, imageType);
	//Image.SCALE_SMOOTH 的缩略算法 生成缩略图片的平滑度的优先级比速度高 生成的图片质量比较好 但速度慢
	Image scaledImage = img.getScaledInstance(newWidth, newHeight, Image.SCALE_REPLICATE);
	tag.getGraphics().drawImage(scaledImage, 0, 0, null);
	
	return tag;
}
 
Example 4
Project: PiCameraProject   File: ImagesEndpoint.java   View Source Code Vote up 6 votes
@GET
@Path("/thumb/{date}")
@Produces("image/jpeg")
public Response photo(@PathParam("date") String whenStr){
    final java.nio.file.Path path = find(whenStr);
    if(!Files.exists(path)){
        return Response.status(Response.Status.NOT_FOUND).build();
    }
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    
    try(InputStream in = Files.newInputStream(path)) {
        final BufferedImage image = ImageIO.read(in);
        final byte[] b = new byte[2048];
        //for(int length=in.read(b); length>0; length=in.read(b)){
            //stream.write(b, 0, length);
        //}
        final int width=320, height=240;
        final Image scaled = image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage dimg = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        dimg.getGraphics().drawImage(scaled, 0, 0, null);
        ImageIO.write(dimg, "JPG", stream);
    } catch (IOException ex) {
        Logger.getLogger(ImagesEndpoint.class.getName()).log(Level.SEVERE, null, ex);
    }
    return Response.ok(stream.toByteArray()).build();
}
 
Example 5
Project: fish-admin   File: ImageUtil.java   View Source Code Vote up 6 votes
/**
 *
 * @param is 输入流,原图片
 * @param fileType 图片类型 jpg, png
 * @param width 要截取的宽度
 * @param height 要截取的高度
 * @return
 * @throws IOException
 */
public static byte[] compress(InputStream is, String fileType, int width, int height) throws IOException {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    BufferedImage src = ImageIO.read(is);
    int picWidth = src.getWidth();   //得到图片的宽度
    int picHeight = src.getHeight();  //得到图片的高度
    float ratio = scale(picWidth, picHeight, width, height);
    int nWidth = (int)(picWidth * ratio);
    int nHeight = (int)(picHeight * ratio);
    Image image = src.getScaledInstance(nWidth, nHeight, Image.SCALE_SMOOTH);
    BufferedImage outputImage = new BufferedImage(nWidth, nHeight, BufferedImage.TYPE_INT_RGB);
    outputImage.getGraphics().drawImage(image, 0, 0, null);
    outputImage.getGraphics().dispose();

    ImageIO.write(outputImage, fileType, byteArrayOutputStream);
    return byteArrayOutputStream.toByteArray();
}
 
Example 6
Project: brModelo   File: baseDrawerItem.java   View Source Code Vote up 6 votes
public void DrawImagem(Graphics2D g) {
    BufferedImage img = getImagem();
    if (img == null) {
        return;
    }
    int[] pts = ArrayDePontos(getPosiImagem());
    if (pts.length != 4) {
        posiImagem = "L,T,200,200";
        imgres = null;
        pts = ArrayDePontos(getPosiImagem());
    }
    Rectangle rec = new Rectangle(pts[0], pts[1], pts[2], pts[3]);
    rec.grow(-2, -2);
    if (imgres == null) {
        imgres = img.getScaledInstance(rec.width, rec.height, Image.SCALE_SMOOTH);
    }
    g.drawImage(imgres, rec.x, rec.y, null);
}
 
Example 7
Project: QN-ACTR-Release   File: SampleQNAnimation.java   View Source Code Vote up 6 votes
private void createOverlayImage() {
	int height = getBounds().height * 2, width = getBounds().width * 2, qLength = width - (height * 4) / 5, qHeight = (height * 4) / 5, yOffs = (height - qHeight) / 2;
	overlayedShading = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
	Color[] shades = new Color[7], lights = new Color[7];
	for (int i = 0; i < shades.length; i++) {
		shades[i] = new Color(0, 0, 0, 160 - (i * 160) / shades.length);
		lights[i] = new Color(255, 255, 255, 200 - (i * 200) / lights.length);
	}
	Graphics g = overlayedShading.getGraphics();
	for (int i = 0; i < shades.length; i++) {
		g.setColor(lights[i]);
		g.drawPolyline(new int[] { i, i, qLength - i }, new int[] { height - i - yOffs, i + yOffs, i + yOffs }, 3);
		g.drawArc(width - height + i, i, height - 2 * i, height - 2 * i, 45 + 4 * i, 180 - 8 * i);
		g.setColor(shades[i]);
		g.drawPolyline(new int[] { i, qLength - i, qLength - i }, new int[] { height - i - yOffs, height - i - yOffs, i + yOffs }, 2);
		g.drawArc(width - height + i, i, height - 2 * i, height - 2 * i, 225 + 4 * i, 180 - 8 * i);
	}
	overlayedShading = overlayedShading.getScaledInstance(getBounds().width, getBounds().height, Image.SCALE_SMOOTH);
}
 
Example 8
Project: geomapapp   File: LocationInset.java   View Source Code Vote up 6 votes
public LocationInset(XMap map){
	this.map=map;
	BufferedImage img;
	try {
		ClassLoader loader = LocationInset.class.getClassLoader();
		URL url = loader.getResource("org/geomapapp/resources/maps/smallWorld.jpg");
		img = ImageIO.read(url);
	} catch (Exception e) {
		return;
	}
	h = img.getHeight()/2;
	w = img.getWidth()/2;
	img2 = img.getScaledInstance(w, h, BufferedImage.SCALE_DEFAULT);

	img = map.getBaseMap();
	img1 = img.getScaledInstance(w*2, h*2, BufferedImage.SCALE_DEFAULT);
	img3 = img.getScaledInstance(w*3, h*3, BufferedImage.SCALE_DEFAULT);
	arrow=new Arrow(null,null);
}
 
Example 9
Project: GIFKR   File: ImageTools.java   View Source Code Vote up 6 votes
public static BufferedImage resize(BufferedImage img, int width, int height, boolean fast){
	if(img.getWidth()==width && img.getHeight()==height)
		return deepCopy(img);

	width = Math.max(width, 1);
	height = Math.max(height, 1);

	Image resizedImg = img.getScaledInstance(width, height, fast ? Image.SCALE_FAST : Image.SCALE_SMOOTH);
	if(resizedImg instanceof BufferedImage)
		return (BufferedImage)resizedImg;

	BufferedImage imgb=new BufferedImage(width, height, img.getType());
	Graphics2D g=imgb.createGraphics();
	g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, fast ? RenderingHints.VALUE_INTERPOLATION_BILINEAR: RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
	g.drawImage(resizedImg,0, 0, null);

	g.dispose();

	return imgb;
}
 
Example 10
Project: zooracle   File: ImgUtils.java   View Source Code Vote up 6 votes
public static Image getThumbnailAsImage(String originalFileName, int width, int height) {
	
	String outputFileName = originalFileName.substring(0, originalFileName.length()-4) + ".tn.jpg";
	Image thumbnailImage = null;
	
	try{
		File thumbnailFile = new File(outputFileName);
		if (thumbnailFile.exists())
			thumbnailImage = ImageIO.read(thumbnailFile);
		else
		{
			BufferedImage img = ImageIO.read(new File(originalFileName));
			thumbnailImage = img.getScaledInstance(width, height, Image.SCALE_SMOOTH);
			ImageIO.write(ImgUtils.toBufferedImage(thumbnailImage), "jpg", thumbnailFile);
		}
		return thumbnailImage;
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	return null;
}
 
Example 11
Project: jmt   File: DefaultIconsToolkit.java   View Source Code Vote up 5 votes
@Override
public Image getJobIcon(Rectangle bounds) {
	int width = 100, height = 100;
	BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
	Graphics g = bi.getGraphics();
	g.setColor(new Color(0, 0, 0, 50));
	g.fillOval(0, 0, width, height);
	for (int i = 0, monoChannel = 0; i < 10; i++, monoChannel = (int) ((1 - Math.exp(-i * 0.5)) * 255)) {
		g.setColor(new Color(monoChannel, monoChannel, monoChannel, 255));
		int r = (int) Math.pow(i, 1.5), s = (int) (r * 2.9);
		g.fillOval(r, r, width - s, height - s);
	}
	return bi.getScaledInstance(bounds.width, bounds.height, Image.SCALE_SMOOTH);
}
 
Example 12
Project: Java-Swing-Helper   File: SwingWindow.java   View Source Code Vote up 5 votes
/**
 * Scale the given icon to the given dimensions
 * 
 * @param label
 * @return
 */
public ImageIcon getScaledImageIcon(Icon icon, int width, int height) {
	if (icon != null) {
		BufferedImage buf = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),
				BufferedImage.TYPE_INT_RGB);
		Graphics graphics = buf.createGraphics();
		icon.paintIcon(null, graphics, 0, 0);
		graphics.dispose();

		Image dimg = buf.getScaledInstance(width, height, Image.SCALE_SMOOTH);
		return new ImageIcon(dimg);
	}
	return null;
}
 
Example 13
Project: UniqR   File: JavaSEPlatform.java   View Source Code Vote up 5 votes
@NotNull
@Override
public Canvas<BufferedImage> createScaled(@NotNull BufferedImage input, int width, int height, int padding, int qrBackgroundColor) {
    final Image scaled = input.getScaledInstance(width - padding * 2, height - padding * 2, Image.SCALE_DEFAULT);
    // Create a buffered image with transparency
    final BufferedImage buffered = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    // Draw the image on to the buffered image
    Graphics2D bGr = buffered.createGraphics();
    bGr.setBackground(new Color(qrBackgroundColor, true));
    bGr.drawImage(scaled, padding, padding, null);
    bGr.dispose();
    scaled.flush();
    return new JavaSECanvas(buffered);
}
 
Example 14
Project: cognitive-services-java-computer-vision-tutorial   File: MainFrame.java   View Source Code Vote up 5 votes
/**
 * Scales the given image to fit the label dimensions.
 * @param bImage: The image to fit.
 * @param label: The label to display the image.
 */
private void scaleAndShowImage(BufferedImage bImage, JLabel label) {
    int bImageHeight = bImage.getHeight();
    int bImageWidth = bImage.getWidth();
    int labelHeight = label.getHeight();
    int labelWidth = label.getWidth();
    
    // Does this need to be scaled?
    if (labelHeight >= bImageHeight && labelWidth >= bImageWidth) {
        // If not, display the image and return.
        ImageIcon image = new ImageIcon(bImage);
        label.setIcon(image);
        return;
    }
    
    // Calculate the new width and height for the image.
    int newHeight;
    int newWidth;
    double bImageAspect = (double)bImageHeight / (double)bImageWidth;
    double labelAspect = (double)labelHeight / (double)labelWidth;
    
    if (bImageAspect > labelAspect) {
        newHeight = labelHeight;
        newWidth = (int)(((double)labelHeight / (double)bImageHeight) * (double)bImageWidth);
    } else {
        newWidth = labelWidth;
        newHeight = (int)(((double)labelWidth / (double)bImageWidth) * (double)bImageHeight);
    }
    
    // Create a new image scaled to the correct size.
    Image newImage = bImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
    
    // Display the scaled image.
    ImageIcon labelImage = new ImageIcon(newImage);
    label.setIcon(labelImage);
    label.validate();
    label.repaint();
}
 
Example 15
Project: document-management-store-app   File: ImageThumbnailCreator.java   View Source Code Vote up 5 votes
public static BufferedImage resizeImage(BufferedImage bufferedImage) {
    try {
        Image scaledInstance = bufferedImage.getScaledInstance(DEFAULT_WIDTH, aspectRatio(bufferedImage), Image.SCALE_SMOOTH);

        BufferedImage resizedBuffedImage = new BufferedImage(
            scaledInstance.getWidth(null),
            scaledInstance.getHeight(null),
            BufferedImage.TYPE_INT_RGB
        );

        Graphics2D resizedBuffedImageGraphics = resizedBuffedImage.createGraphics();

        // Apply scaled Image to the Buffered Image Graphic.
        resizedBuffedImageGraphics.drawImage(
            scaledInstance,
            0,
            0,
            scaledInstance.getWidth(null),
            scaledInstance.getHeight(null),
            Color.WHITE,
            null
        );

        resizedBuffedImageGraphics.dispose();

        return resizedBuffedImage;
    } catch (Exception e) {
        throw new CantCreateThumbnailException(e);
    }
}
 
Example 16
Project: QN-ACTR-Release   File: DefaultIconsToolkit.java   View Source Code Vote up 5 votes
@Override
public Image getJobIcon(Rectangle bounds) {
	int width = 100, height = 100;
	BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
	Graphics g = bi.getGraphics();
	g.setColor(new Color(0, 0, 0, 50));
	g.fillOval(0, 0, width, height);
	for (int i = 0, monoChannel = 0; i < 10; i++, monoChannel = (int) ((1 - Math.exp(-i * 0.5)) * 255)) {
		g.setColor(new Color(monoChannel, monoChannel, monoChannel, 255));
		int r = (int) Math.pow(i, 1.5), s = (int) (r * 2.9);
		g.fillOval(r, r, width - s, height - s);
	}
	return bi.getScaledInstance(bounds.width, bounds.height, Image.SCALE_SMOOTH);
}
 
Example 17
Project: lemon-framework   File: ImageUtil.java   View Source Code Vote up 5 votes
/**
 * 按比例裁剪
 * @param img
 * @param targetWidth
 * @param targetHeight
 * @return
 */
public static BufferedImage getScaleCutscale(BufferedImage img, int targetWidth, int targetHeight) {
	int srcWidth = img.getWidth();
	int srcHeight = img.getHeight();
	//当超过图片最大宽高时候等比缩小所需要的图片规格
	while(targetWidth>srcWidth || targetHeight>srcHeight){
		double rt = (targetWidth>srcWidth)? ((double)targetWidth)/(srcWidth): ((double)targetHeight)/(srcHeight);
		targetWidth = (int)(((double)targetWidth)/rt);
		targetHeight = (int)(((double)targetHeight)/rt);
	}

	double rate1 = ((double)srcWidth)/(targetWidth);
	double rate2 = ((double)srcHeight)/(targetHeight);
       // 根据缩放比率大的进行缩放控制 
       double rate = rate1 < rate2 ? rate1 : rate2; 
       int newWidth = (int)(((double)srcWidth)/rate);
       int newHeight = (int)(((double)srcHeight)/rate);

       int x1 = newWidth/2 - targetWidth/2;
       int x2 = newWidth/2 + targetWidth/2;
       int y1 = newHeight/2 - targetHeight/2;
       int y2 = newHeight/2 + targetHeight/2;
	int imageType = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
	BufferedImage tag = new BufferedImage(targetWidth, targetHeight, imageType);
    Image scaledImage = img.getScaledInstance(newWidth, newHeight, Image.SCALE_REPLICATE);
	tag.getGraphics().drawImage(scaledImage, 0, 0, targetWidth, targetHeight, x1, y1, x2, y2, null);
	
	return tag;
}
 
Example 18
Project: McLink   File: ImageUtil.java   View Source Code Vote up 5 votes
public static BufferedImage resize(BufferedImage img, int newW, int newH) { 
	Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
	BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);

	Graphics2D g2d = dimg.createGraphics();
	g2d.drawImage(tmp, 0, 0, null);
	g2d.dispose();

	return dimg;
}
 
Example 19
Project: anagnostes   File: Normalizer.java   View Source Code Vote up 5 votes
public static BufferedImage resize(BufferedImage img, int newW, int newH) {
	Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
	BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_BYTE_GRAY);
	Graphics2D g2d = dimg.createGraphics();
	g2d.drawImage(tmp, 0, 0, null);
	g2d.dispose();
	return dimg;
}
 
Example 20
Project: pm-home-station   File: NativeTrayIntegration.java   View Source Code Vote up 4 votes
private Image getIcon(String name) throws IOException {
    BufferedImage trayIconImage = ResourceHelper.getAppIcon(name);
    int trayIconWidth = new TrayIcon(trayIconImage).getSize().width;
    return trayIconImage.getScaledInstance(trayIconWidth, -1, Image.SCALE_SMOOTH); // scaling is better for Win7
}