java.awt.image.AffineTransformOp#filter()源码实例Demo

下面列出了java.awt.image.AffineTransformOp#filter() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: r2cloud   文件: Util.java
public static void rotateImage(File result) {
	try {
		BufferedImage image;
		try (FileInputStream fis = new FileInputStream(result)) {
			image = ImageIO.read(fis);
		}
		AffineTransform tx = AffineTransform.getScaleInstance(-1, -1);
		tx.translate(-image.getWidth(null), -image.getHeight(null));
		AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
		image = op.filter(image, null);
		try (FileOutputStream fos = new FileOutputStream(result)) {
			ImageIO.write(image, "jpg", fos);
		}
	} catch (Exception e) {
		LOG.error("unable to rotate image", e);
	}
}
 
源代码2 项目: markdown-image-kit   文件: ImageUtils.java
/**
 * Scale image buffered image.
 *
 * @param sourceImage the source image
 * @param newWidth    the new width
 * @param newHeight   the new height
 * @return the buffered image
 */
@Contract("null, _, _ -> null")
public static BufferedImage scaleImage(BufferedImage sourceImage, int newWidth, int newHeight) {
    if (sourceImage == null) {
        return null;
    }

    if (newWidth == 0 || newHeight == 0) {
        return null;
    }

    AffineTransform at = AffineTransform.getScaleInstance((double) newWidth / sourceImage.getWidth(null),
                                                          (double) newHeight / sourceImage.getHeight(null));

    //  http://nickyguides.digital-digest.com/bilinear-vs-bicubic.htm
    AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
    return op.filter(sourceImage, null);
}
 
源代码3 项目: JGiven   文件: Html5AttachmentGenerator.java
static BufferedImage scaleDown( BufferedImage before ) {
    double xFactor = Math.min(1.0, MINIMAL_THUMBNAIL_SIZE / (double) before.getWidth());
    double yFactor = Math.min(1.0, MINIMAL_THUMBNAIL_SIZE / (double) before.getHeight());

    double factor = Math.max(xFactor, yFactor);

    int width = (int) Math.round(before.getWidth() * factor);
    int height = (int) Math.round(before.getHeight() * factor);
    BufferedImage after = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB );

    AffineTransform at = new AffineTransform();
    at.scale( factor, factor );
    AffineTransformOp scaleOp = new AffineTransformOp( at, AffineTransformOp.TYPE_BILINEAR );

    return scaleOp.filter( before, after );
}
 
/**
 * Some quick and dirty image scaling - please note that for best performance
 * and quality you should use image rescaling libraries.
 */
@Override
public BufferedImage resample(BufferedImage bufferedImage, int width, int height) {

    Dimension imageDimension = new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight());
    Dimension boundaryDimension = new Dimension(width, height);
    Dimension scaledDimension = BufferedImageUtils.getScaledDimension(imageDimension, boundaryDimension);

    double scaleX = scaledDimension.getWidth() / bufferedImage.getWidth();
    double scaleY = scaledDimension.getHeight() / bufferedImage.getHeight();

    AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    AffineTransformOp biLinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);

    return biLinearScaleOp.filter(
            bufferedImage,
            new BufferedImage(scaledDimension.width, scaledDimension.height, bufferedImage.getType()));
}
 
源代码5 项目: M2Doc   文件: ImageServices.java
@Documentation(
    value = "Resizes the Image by the given factor.",
    params = {
        @Param(name = "image", value = "The Image"),
        @Param(name = "factor", value = "The resize factor"),
    },
    result = "resize the image",
    examples = {
        @Example(expression = "myImage.resize(0.5)", result = "will resize the image by a factor 0.5"),
    }
)
// @formatter:on
public MImage resize(MImage image, Double factor) throws IOException {
    final BufferedImage bufferedImage = MImageAWTImpl.getBufferedImage(image);

    final BufferedImage resized = new BufferedImage((int) (bufferedImage.getWidth() * factor),
            (int) (bufferedImage.getHeight() * factor), bufferedImage.getType());

    final AffineTransform zoomTransfort = AffineTransform.getScaleInstance(factor, factor);
    final AffineTransformOp retaillerImage = new AffineTransformOp(zoomTransfort, AffineTransformOp.TYPE_BILINEAR);
    retaillerImage.filter(bufferedImage, resized);

    return new MImageAWTImpl(resized, image.getURI());
}
 
/**
 * Scales the signature image to fit the provided signature field dimensions,
 * preserving the aspect ratio
 * 
 * @param signatureImage
 * @param width
 * @param height
 * @return
 */
private BufferedImage scaleSignature(BufferedImage signatureImage, int width, int height)
{
	if(signatureImage.getHeight() > height)
	{
 	BufferedImage scaled = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
 	AffineTransform at = new AffineTransform();
 	at.scale(2.0, 2.0);
 	AffineTransformOp scaleOp = 
 	   new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
 	scaled = scaleOp.filter(signatureImage, scaled);
 	return scaled;
	}
	else
	{
		return signatureImage;
	}
}
 
源代码7 项目: Dayon   文件: Assistant.java
private BufferedImage scaleImage(BufferedImage image, int width, int height) {
    double scaleX = (double)width/image.getWidth();
    double scaleY = (double)height/image.getHeight();
    AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);
    return bilinearScaleOp.filter(image, new BufferedImage(width, height, image.getType()));
}
 
源代码8 项目: easybuggy   文件: ImagingOpExceptionServlet.java
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    BufferedImage img = new BufferedImage(1, 40000, BufferedImage.TYPE_INT_RGB);
    AffineTransformOp flipAtop = new AffineTransformOp(AffineTransform.getScaleInstance(1, 1),
            AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    flipAtop.filter(img, null);
}
 
源代码9 项目: util.commons   文件: ImageZoomUtils.java
/**
 * 按比例放缩图片
 * 
 * @param sourcePath
 *      原图片路径
 * @param targetPath
 *      目标图片路径
 * @param ratio
 *      放缩比率
 * @throws IOException
 *      读写异常
 */
public void zoomImage(String sourcePath, String targetPath, double ratio) throws IOException {
    int zoomWidth = 0, zoomHeight = 0;
    File srcFile = new File(sourcePath);
    File destFile = new File(targetPath);
    
    BufferedImage bufferedImage = ImageIO.read(srcFile);
    zoomWidth = (int) (ratio * bufferedImage.getWidth());
    zoomHeight = (int) (ratio * bufferedImage.getHeight());
    
    Image Itemp = bufferedImage.getScaledInstance(zoomWidth, zoomHeight, BufferedImage.SCALE_SMOOTH);
    AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
    Itemp = ato.filter(bufferedImage, null);
    ImageIO.write((BufferedImage) Itemp, targetPath.substring(targetPath.lastIndexOf(".") + 1), destFile);
}
 
源代码10 项目: oim-fx   文件: ImageUtil.java
/**
 * 图片缩放
 * 
 * @param filePath
 *            图片路径
 * @param height
 *            高度
 * @param width
 *            宽度
 * @param bb
 *            比例不对时是否需要补白
 */
public static void resize(String filePath, int height, int width, boolean bb) {
	try {
		double ratio = 0; // 缩放比例
		File f = new File(filePath);
		BufferedImage bi = ImageIO.read(f);
		Image itemp = bi.getScaledInstance(width, height, BufferedImage.SCALE_SMOOTH);
		// 计算比例
		if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
			if (bi.getHeight() > bi.getWidth()) {
				ratio = (new Integer(height)).doubleValue() / bi.getHeight();
			} else {
				ratio = (new Integer(width)).doubleValue() / bi.getWidth();
			}
			AffineTransformOp op = new AffineTransformOp(AffineTransform.getScaleInstance(ratio, ratio), null);
			itemp = op.filter(bi, null);
		}
		if (bb) {
			BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D g = image.createGraphics();
			g.setColor(Color.white);
			g.fillRect(0, 0, width, height);
			if (width == itemp.getWidth(null)) {
				g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
			} else {
				g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0, itemp.getWidth(null), itemp.getHeight(null), Color.white, null);
			}
			g.dispose();
			itemp = image;
		}
		ImageIO.write((BufferedImage) itemp, "jpg", f);
	} catch (IOException e) {
		e.printStackTrace();
	}
}
 
源代码11 项目: MtgDesktopCompanion   文件: FlipActions.java
@Override
public void actionPerformed(ActionEvent e) {
	try {
		MagicCard mc = MTGControler
				.getInstance().getEnabled(MTGCardsProvider.class).searchCardByName(
						card.getMagicCard().getRotatedCardName(), card.getMagicCard().getCurrentSet(), true)
				.get(0);
		card.setMagicCard(mc);
		
		BufferedImage bufferedImage = new BufferedImage(card.getWidth(), card.getHeight(),BufferedImage.TYPE_INT_RGB);
		AffineTransform tx = AffineTransform.getScaleInstance(-1, -1);
						tx.translate(-bufferedImage.getWidth(null), -bufferedImage.getHeight(null));
		AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
		bufferedImage = op.filter(bufferedImage, null);

		Graphics2D g2 = bufferedImage.createGraphics();
		g2.drawImage(card.getImageIcon().getImage(), tx, null);
		g2.dispose();

		card.setImage(new ImageIcon(bufferedImage));
		card.setRotated(true);
		card.revalidate();
		card.repaint();

		card.initActions();
		GamePanelGUI.getInstance().getPlayer().logAction("Flip " + card.getMagicCard());
	} catch (Exception ex) {
		logger.error(ex);
	}

}
 
源代码12 项目: buffer_bci   文件: MeterPanel.java
/**
 * Paints an arrow in a given direction (from 0 to 2*pi).
 * @param g
 * @param direction 
 */
protected void paintArrow(Graphics g, double direction)
{        
    Graphics2D g2d = (Graphics2D)g;
    int size = (this.getWidth() > this.getHeight()) ? this.getHeight() : this.getWidth();
    
    // Arrow.
    BufferedImage image = this.getImageArrow();
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();
    double imagePreferredSize = this.getConfiguration().getDouble("arrow.size") * size;
    double imageScaleFactor = (imageWidth > imageHeight) ? imagePreferredSize / imageWidth : imagePreferredSize / imageHeight;
       
    double rotationSin = Math.abs(Math.sin(direction));
    double rotationCos = Math.abs(Math.cos(direction));
    int imageNewWidth = (int)Math.floor(imageWidth * rotationCos + imageHeight * rotationSin);
    int imageNewHeight = (int)Math.floor(imageHeight * rotationCos + imageWidth * rotationSin);
    
    AffineTransform transform = new AffineTransform();
    transform.scale(imageScaleFactor, imageScaleFactor);
    transform.translate((imageNewWidth - imageWidth) / 2, (imageNewHeight - imageHeight) / 2);
    transform.rotate(direction, imageWidth / 2, imageHeight / 2);
    
    AffineTransformOp operation = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
    image = operation.filter(image, null);

    g2d.drawImage(image,
                 (int)((this.getWidth() - image.getWidth()) / 2),
                 (int)((this.getHeight() - image.getHeight()) / 2),
                 null);
}
 
public static Raster testRasterTransform(Raster src, WritableRaster dst,
                                         AffineTransform transform) {
    AffineTransformOp op =
            new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);

    return op.filter(src, dst);
}
 
源代码14 项目: coordination_oru   文件: RVizVisualization.java
private BufferedImage flipVertically(BufferedImage imgIn) {
       AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
       tx.translate(0, -imgIn.getHeight(null));
       AffineTransformOp op1 = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
       imgIn = op1.filter(imgIn, null);
       return imgIn;
}
 
源代码15 项目: seed   文件: ImageUtil.java
public void affineTransform (double fShxFactor, double fShyFactor) {

        try {
          AffineTransform shearer =
            AffineTransform.getShearInstance (fShxFactor, fShyFactor);
          AffineTransformOp shear_op =
            new AffineTransformOp (shearer, null);
          bufferedImage = shear_op.filter (bufferedImage, null);
        }
        catch (Exception e) {
          System.out.println("Shearing exception = " + e);
        }
      }
 
源代码16 项目: scrimage   文件: ImmutableImage.java
/**
 * Applies an affine transform in place.
 */
private BufferedImage affineTransform(AffineTransform tx) {
   AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
   return op.filter(awt(), null);
}
 
源代码17 项目: util   文件: ImageUtil.java
/**
 * 缩放图像(按高度和宽度缩放)
 * @param srcImageFile 源图像文件地址
 * @param result 缩放后的图像地址
 * @param height 缩放后的高度
 * @param width 缩放后的宽度
 * @param bb 比例不对时是否需要补白:true为补白; false为不补白;
 */
public final static void scale2(String srcImageFile, String result, int height, int width, boolean bb) {
    try {
        double ratio = 0.0; // 缩放比例
        File f = new File(srcImageFile);
        BufferedImage bi = ImageIO.read(f);
        Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
        // 计算比例
        if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
            if (bi.getHeight() > bi.getWidth()) {
                ratio = (new Integer(height)).doubleValue()
                        / bi.getHeight();
            } else {
                ratio = (new Integer(width)).doubleValue() / bi.getWidth();
            }
            AffineTransformOp op = new AffineTransformOp(AffineTransform
                    .getScaleInstance(ratio, ratio), null);
            itemp = op.filter(bi, null);
        }
        if (bb) {//补白
            BufferedImage image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, width, height);
            if (width == itemp.getWidth(null))
                g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
                        itemp.getWidth(null), itemp.getHeight(null),
                        Color.white, null);
            else
                g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
                        itemp.getWidth(null), itemp.getHeight(null),
                        Color.white, null);
            g.dispose();
            itemp = image;
        }
        ImageIO.write((BufferedImage) itemp, "JPEG", new File(result));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
源代码18 项目: MobyDroid   文件: JPanel_ScreenCapture.java
private void drawImage() {
    // check first
    if (iconBytes == null || iconBytes.length == 0) {
        return;
    }

    // load bufered image
    BufferedImage bi;
    try {
        bi = ImageIO.read(new ByteArrayInputStream(iconBytes));
    } catch (IOException ex) {
        return;
    }

    // rotate
    if (rotationAngle != 0) {
        AffineTransform tx = new AffineTransform();
        tx.rotate(Math.PI * rotationAngle / 2.0, bi.getWidth() / 2.0, bi.getHeight() / 2.0);
        if (rotationAngle == 1 || rotationAngle == 3) {
            double offset;
            if (rotationAngle == 1) {
                offset = (bi.getWidth() - bi.getHeight()) / 2.0;
            } else {
                offset = (bi.getHeight() - bi.getWidth()) / 2.0;
            }
            tx.translate(offset, offset);
        }
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        bi = op.filter(bi, null);
    }

    // get width and height
    int width = bi.getWidth();
    int height = bi.getHeight();

    // first check if we need to scale width
    if (bi.getWidth() > jLabel_ScreenShot.getWidth()) {
        //scale width to fit
        width = jLabel_ScreenShot.getWidth();
        //scale height to maintain aspect ratio
        height = (width * bi.getHeight()) / bi.getWidth();
    }
    // then check if we need to scale even with the new height
    if (height > jLabel_ScreenShot.getHeight()) {
        //scale height to fit instead
        height = jLabel_ScreenShot.getHeight();
        //scale width to maintain aspect ratio
        width = (height * bi.getWidth()) / bi.getHeight();
    }

    // set image
    jLabel_ScreenShot.setIcon(new ImageIcon(bi.getScaledInstance(width, height, Image.SCALE_DEFAULT)));
}
 
源代码19 项目: JavaLib   文件: ImageUtils.java
/**
 * 图片裁剪
 * @param filePath 原图片
 * @param width 宽
 * @param height 高
 * @return BufferedImage
 * @throws Exception 异常
 */
public static BufferedImage clipping(String filePath, int width, int height) throws Exception {

    BufferedImage buffer = ImageIO.read(new File(filePath));

    /*
     * 核心算法,计算图片的压缩比
     */
    int w= buffer.getWidth();
    int h=buffer.getHeight();
    double ratiox = 1.0d;
    double ratioy = 1.0d;

    ratiox= w * ratiox / width;
    ratioy= h * ratioy / height;

    if( ratiox >= 1){
        if(ratioy < 1){
            ratiox = height * 1.0 / h;
        }else{
            if(ratiox > ratioy){
                ratiox = height * 1.0 / h;
            }else{
                ratiox = width * 1.0 / w;
            }
        }
    }else{
        if(ratioy < 1){
            if(ratiox > ratioy){
                ratiox = height * 1.0 / h;
            }else{
                ratiox = width * 1.0 / w;
            }
        }else{
            ratiox = width * 1.0 / w;
        }
    }
    /*
     * 对于图片的放大或缩小倍数计算完成,ratiox大于1,则表示放大,否则表示缩小
     */
    AffineTransformOp op = new AffineTransformOp(AffineTransform
            .getScaleInstance(ratiox, ratiox), null);
    buffer = op.filter(buffer, null);
    //从放大的图像中心截图
    buffer = buffer.getSubimage((buffer.getWidth()-width)/2, (buffer.getHeight() - height) / 2, width, height);

    // 将 buffer 转成 图片

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageIO.write(buffer, "jpg", out);

    return buffer;
}
 
源代码20 项目: albert   文件: ImageUtils.java
/***
 * 缩放图像,按照长宽缩放
 * @param srcImageFile
 * @param result
 * @param height  变换后的高度
 * @param width   变换后的长度
 * @param bb      比例不对称时,是否补白,true 补白;false 不补白
 * @return
 * @date   2016-3-30下午2:44:37
 */
public static boolean scale2(String srcImageFile, String result, int height, int width, boolean bb) {
    try {
    	
        double ratio = 0.0; // 缩放比例
        File file = new File(srcImageFile);
        BufferedImage bi = ImageIO.read(file); // 读入文件
        Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("jpg");
        ImageReader reader = (ImageReader) readers.next();
        ImageInputStream iis = ImageIO.createImageInputStream(file);
        reader.setInput(iis, true);
        int width1 = reader.getWidth(0); // 得到源图宽
        int height1 = reader.getHeight(0); // 得到源图长
        @SuppressWarnings("static-access")
		Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);
        // 计算比例
        if ((height1 > height) || (width1 > width)) {
            if (height1 > width1) {
                ratio = (new Integer(height)).doubleValue()
                        / height1;
            } else {
                ratio = (new Integer(width)).doubleValue() / width1;
            }
            AffineTransformOp op = new AffineTransformOp(AffineTransform
                    .getScaleInstance(ratio, ratio), null);
            itemp = op.filter(bi, null);
        }
        if (bb) {//补白
            BufferedImage image = new BufferedImage(width, height,
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = image.createGraphics();
            g.setColor(Color.white);
            g.fillRect(0, 0, width, height);
            if (width == itemp.getWidth(null))
                g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
                        itemp.getWidth(null), itemp.getHeight(null),
                        Color.white, null);
            else
                g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
                        itemp.getWidth(null), itemp.getHeight(null),
                        Color.white, null);
            g.dispose();
            itemp = image;
        }
        ImageIO.write((BufferedImage) itemp, "JPEG", new File(result));
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}