当前位置: 首页>>代码示例>>Java>>正文


Java BufferedImage.getScaledInstance方法代码示例

本文整理汇总了Java中java.awt.image.BufferedImage.getScaledInstance方法的典型用法代码示例。如果您正苦于以下问题:Java BufferedImage.getScaledInstance方法的具体用法?Java BufferedImage.getScaledInstance怎么用?Java BufferedImage.getScaledInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.awt.image.BufferedImage的用法示例。


在下文中一共展示了BufferedImage.getScaledInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: prepareImage

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
@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;
}
 
开发者ID:erikcostlow,项目名称:PiCameraProject,代码行数:26,代码来源:ErikDetectorAlgorithm.java

示例2: createOverlayImage

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
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);
}
 
开发者ID:max6cn,项目名称:jmt,代码行数:20,代码来源:SampleQNAnimation.java

示例3: getScaleZoom

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
/**
 * 按比例缩放
 * @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;
}
 
开发者ID:lemon-china,项目名称:lemon-framework,代码行数:29,代码来源:ImageUtil.java

示例4: photo

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
@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();
}
 
开发者ID:erikcostlow,项目名称:PiCameraProject,代码行数:27,代码来源:ImagesEndpoint.java

示例5: compress

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
/**
 *
 * @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();
}
 
开发者ID:myliang,项目名称:fish-admin,代码行数:27,代码来源:ImageUtil.java

示例6: DrawImagem

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
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);
}
 
开发者ID:chcandido,项目名称:brModelo,代码行数:19,代码来源:baseDrawerItem.java

示例7: LocationInset

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
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);
}
 
开发者ID:iedadata,项目名称:geomapapp,代码行数:20,代码来源:LocationInset.java

示例8: resize

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
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;
}
 
开发者ID:CalebKussmaul,项目名称:GIFKR,代码行数:21,代码来源:ImageTools.java

示例9: getThumbnailAsImage

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
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;
}
 
开发者ID:fossasia,项目名称:zooracle,代码行数:24,代码来源:ImgUtils.java

示例10: getJobIcon

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
@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);
}
 
开发者ID:max6cn,项目名称:jmt,代码行数:15,代码来源:DefaultIconsToolkit.java

示例11: getScaledImageIcon

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
/**
 * 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;
}
 
开发者ID:Cyphereion,项目名称:Java-Swing-Helper,代码行数:20,代码来源:SwingWindow.java

示例12: createScaled

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
@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);
}
 
开发者ID:mariotaku,项目名称:UniqR,代码行数:15,代码来源:JavaSEPlatform.java

示例13: scaleAndShowImage

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
/**
 * 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();
}
 
开发者ID:Azure-Samples,项目名称:cognitive-services-java-computer-vision-tutorial,代码行数:43,代码来源:MainFrame.java

示例14: resizeImage

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
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);
    }
}
 
开发者ID:hmcts,项目名称:document-management-store-app,代码行数:31,代码来源:ImageThumbnailCreator.java

示例15: getScaleCutscale

import java.awt.image.BufferedImage; //导入方法依赖的package包/类
/**
 * 按比例裁剪
 * @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;
}
 
开发者ID:lemon-china,项目名称:lemon-framework,代码行数:36,代码来源:ImageUtil.java


注:本文中的java.awt.image.BufferedImage.getScaledInstance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。