Search in sources :

Example 1 with CropImageFilter

use of java.awt.image.CropImageFilter in project hutool by looly.

the class Img method cut.

/**
 * 图像切割(按指定起点坐标和宽高切割)
 *
 * @param rectangle 矩形对象,表示矩形区域的x,y,width,height
 * @return this
 */
public Img cut(Rectangle rectangle) {
    final Image srcImage = getValidSrcImg();
    fixRectangle(rectangle, srcImage.getWidth(null), srcImage.getHeight(null));
    final ImageFilter cropFilter = new CropImageFilter(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
    this.targetImage = ImgUtil.filter(cropFilter, srcImage);
    return this;
}
Also used : CropImageFilter(java.awt.image.CropImageFilter) ImageFilter(java.awt.image.ImageFilter) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) CropImageFilter(java.awt.image.CropImageFilter)

Example 2 with CropImageFilter

use of java.awt.image.CropImageFilter in project exist by eXist-db.

the class CropFunction method eval.

/**
 * evaluate the call to the xquery crop() function,
 * it is really the main entry point of this class
 *
 * @param args		arguments from the crop() function call
 * @param contextSequence	the Context Sequence to operate on (not used here internally!)
 * @return		A sequence representing the result of the crop() function call
 *
 * @see org.exist.xquery.BasicFunction#eval(org.exist.xquery.value.Sequence[], org.exist.xquery.value.Sequence)
 */
@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {
    // was an image and a mime-type speficifed
    if (args[0].isEmpty() || args[2].isEmpty()) {
        return Sequence.EMPTY_SEQUENCE;
    }
    // get the maximum dimensions to crop to
    int x1 = 0;
    int y1 = 0;
    int x2 = MAXHEIGHT;
    int y2 = MAXWIDTH;
    int width = 0;
    int height = 0;
    if (!args[1].isEmpty()) {
        x1 = ((IntegerValue) args[1].itemAt(0)).getInt();
        if (args[1].hasMany()) {
            y1 = ((IntegerValue) args[1].itemAt(1)).getInt();
            x2 = ((IntegerValue) args[1].itemAt(2)).getInt();
            y2 = ((IntegerValue) args[1].itemAt(3)).getInt();
            width = x2 - x1;
            height = y2 - y1;
        }
    }
    if (width < 1) {
        logger.error("cropping error: x2 value must be greater than x1");
        return Sequence.EMPTY_SEQUENCE;
    }
    if (height < 1) {
        logger.error("cropping error: y2 must be greater than y1");
        return Sequence.EMPTY_SEQUENCE;
    }
    // get the mime-type
    String mimeType = args[2].itemAt(0).getStringValue();
    String formatName = mimeType.substring(mimeType.indexOf("/") + 1);
    // TODO currently ONLY tested for JPEG!!!
    BufferedImage bImage = null;
    try (InputStream inputStream = ((BinaryValue) args[0].itemAt(0)).getInputStream()) {
        // get the image data
        Image image = ImageIO.read(inputStream);
        if (image == null) {
            logger.error("Unable to read image data!");
            return Sequence.EMPTY_SEQUENCE;
        }
        // crop the image
        Image cropImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(x1, y1, width, height)));
        if (cropImage instanceof BufferedImage) {
            // just in case cropImage is allready an BufferedImage
            bImage = (BufferedImage) cropImage;
        } else {
            bImage = new BufferedImage(cropImage.getWidth(null), cropImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // Paint the image onto the buffered image
            Graphics2D g = bImage.createGraphics();
            g.drawImage(cropImage, 0, 0, null);
            g.dispose();
        }
        try (final UnsynchronizedByteArrayOutputStream os = new UnsynchronizedByteArrayOutputStream()) {
            ImageIO.write(bImage, formatName, os);
            // return the new croped image data
            return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(), os.toInputStream());
        }
    } catch (Exception e) {
        throw new XPathException(this, e.getMessage());
    }
}
Also used : XPathException(org.exist.xquery.XPathException) BinaryValueFromInputStream(org.exist.xquery.value.BinaryValueFromInputStream) InputStream(java.io.InputStream) BinaryValue(org.exist.xquery.value.BinaryValue) Base64BinaryValueType(org.exist.xquery.value.Base64BinaryValueType) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) UnsynchronizedByteArrayOutputStream(org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream) BufferedImage(java.awt.image.BufferedImage) XPathException(org.exist.xquery.XPathException) Graphics2D(java.awt.Graphics2D) FilteredImageSource(java.awt.image.FilteredImageSource) CropImageFilter(java.awt.image.CropImageFilter)

Example 3 with CropImageFilter

use of java.awt.image.CropImageFilter in project hutool by looly.

the class ImageUtil method cut.

/**
 * 图像切割(按指定起点坐标和宽高切割)
 *
 * @param srcImage 源图像
 * @param rectangle 矩形对象,表示矩形区域的x,y,width,height
 * @return {@link BufferedImage}
 * @since 3.1.0
 */
public static BufferedImage cut(Image srcImage, Rectangle rectangle) {
    final ImageFilter cropFilter = new CropImageFilter(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
    Image img = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(srcImage.getSource(), cropFilter));
    return toBufferedImage(img);
}
Also used : CropImageFilter(java.awt.image.CropImageFilter) ImageFilter(java.awt.image.ImageFilter) FilteredImageSource(java.awt.image.FilteredImageSource) CropImageFilter(java.awt.image.CropImageFilter) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Image (java.awt.Image)3 BufferedImage (java.awt.image.BufferedImage)3 CropImageFilter (java.awt.image.CropImageFilter)3 FilteredImageSource (java.awt.image.FilteredImageSource)2 ImageFilter (java.awt.image.ImageFilter)2 Graphics2D (java.awt.Graphics2D)1 InputStream (java.io.InputStream)1 UnsynchronizedByteArrayOutputStream (org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream)1 XPathException (org.exist.xquery.XPathException)1 Base64BinaryValueType (org.exist.xquery.value.Base64BinaryValueType)1 BinaryValue (org.exist.xquery.value.BinaryValue)1 BinaryValueFromInputStream (org.exist.xquery.value.BinaryValueFromInputStream)1