Java Code Examples for java.awt.image.AffineTransformOp#filter()

The following examples show how to use java.awt.image.AffineTransformOp#filter() . 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: ImageUtils.java    From markdown-image-kit with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: PDFSignatureProviderActionExecuter.java    From CounterSign with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 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;
	}
}
 
Example 3
Source File: Html5AttachmentGenerator.java    From JGiven with Apache License 2.0 6 votes vote down vote up
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 );
}
 
Example 4
Source File: BaseSanselanTest.java    From java-image-processing-survival-guide with Apache License 2.0 6 votes vote down vote up
/**
 * 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()));
}
 
Example 5
Source File: ImageServices.java    From M2Doc with Eclipse Public License 1.0 6 votes vote down vote up
@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());
}
 
Example 6
Source File: Util.java    From r2cloud with Apache License 2.0 6 votes vote down vote up
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);
	}
}
 
Example 7
Source File: FlipActions.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
@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);
	}

}
 
Example 8
Source File: ImageUtil.java    From seed with Apache License 2.0 5 votes vote down vote up
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);
        }
      }
 
Example 9
Source File: RVizVisualization.java    From coordination_oru with GNU General Public License v3.0 5 votes vote down vote up
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;
}
 
Example 10
Source File: InvalidTransformParameterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static Raster testRasterTransform(Raster src, WritableRaster dst,
                                         AffineTransform transform) {
    AffineTransformOp op =
            new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);

    return op.filter(src, dst);
}
 
Example 11
Source File: MeterPanel.java    From buffer_bci with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 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);
}
 
Example 12
Source File: ImageUtil.java    From oim-fx with MIT License 5 votes vote down vote up
/**
 * 图片缩放
 * 
 * @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();
	}
}
 
Example 13
Source File: ImageZoomUtils.java    From util.commons with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 按比例放缩图片
 * 
 * @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);
}
 
Example 14
Source File: ImagingOpExceptionServlet.java    From easybuggy with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 15
Source File: Assistant.java    From Dayon with GNU General Public License v3.0 5 votes vote down vote up
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()));
}
 
Example 16
Source File: JPanel_ScreenCapture.java    From MobyDroid with Apache License 2.0 4 votes vote down vote up
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)));
}
 
Example 17
Source File: ImageUtils.java    From JavaLib with MIT License 4 votes vote down vote up
/**
 * 图片裁剪
 * @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;
}
 
Example 18
Source File: ImageUtil.java    From util with Apache License 2.0 4 votes vote down vote up
/**
 * 缩放图像(按高度和宽度缩放)
 * @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();
    }
}
 
Example 19
Source File: ImmutableImage.java    From scrimage with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
}
 
Example 20
Source File: ImageUtils.java    From albert with MIT License 4 votes vote down vote up
/***
 * 缩放图像,按照长宽缩放
 * @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;
    }
}