Example usage for java.awt.image CropImageFilter CropImageFilter

List of usage examples for java.awt.image CropImageFilter CropImageFilter

Introduction

In this page you can find the example usage for java.awt.image CropImageFilter CropImageFilter.

Prototype

public CropImageFilter(int x, int y, int w, int h) 

Source Link

Document

Constructs a CropImageFilter that extracts the absolute rectangular region of pixels from its source Image as specified by the x, y, w, and h parameters.

Usage

From source file:Crop.java

public Crop() {
    super();//from w  ww .java2s.c  o m
    ImageIcon icon = new ImageIcon("java2s.PNG");
    image = icon.getImage();
    image = createImage(new FilteredImageSource(image.getSource(), new CropImageFilter(73, 63, 141, 131)));
}

From source file:MainClass.java

public void init() {
    MediaTracker mt = new MediaTracker(this);
    i = getImage(getDocumentBase(), "rosey.jpg");
    mt.addImage(i, 0);/*w w  w  . ja v a2  s  .  co m*/
    try {
        mt.waitForAll();
        int width = i.getWidth(this);
        int height = i.getHeight(this);
        j = createImage(new FilteredImageSource(i.getSource(),
                new CropImageFilter(width / 3, height / 3, width / 3, height / 3)));
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:com.ace.erp.controller.sys.company.OrganizationController.java

@RequestMapping(value = "/company/uploadLogo", method = RequestMethod.POST)
@ResponseBody/*from  w  w w  .  ja  v  a 2s  .  c om*/
public Response uploadLogo(@CurrentUser User user, String srcImageFile, int x, int y, int destWidth,
        int destHeight, int srcShowWidth, int srcShowHeight, HttpServletRequest request) {
    try {
        String path = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getContextPath();
        Image img;
        ImageFilter cropFilter;
        //String srcFileName = FilenameUtils.getName(srcImageFile);
        String srcFileName = StringUtils.isNotBlank(contextPath) ? srcImageFile.replaceFirst(contextPath, "")
                : srcImageFile;
        // ???
        File srcFile = new File(path + "/" + srcFileName);

        BufferedImage bi = ImageIO.read(srcFile);
        //??????????
        int srcWidth = bi.getWidth(); // ?
        int srcHeight = bi.getHeight(); // ?
        if (srcShowWidth == 0)
            srcShowWidth = srcWidth;
        if (srcShowHeight == 0)
            srcShowHeight = srcHeight;

        if (srcShowWidth >= destWidth && srcShowHeight >= destHeight) {
            //???
            Image image = bi.getScaledInstance(srcShowWidth, srcShowHeight, Image.SCALE_DEFAULT);

            cropFilter = new CropImageFilter(x, y, destWidth, destHeight);

            img = Toolkit.getDefaultToolkit()
                    .createImage(new FilteredImageSource(image.getSource(), cropFilter));
            BufferedImage tag = new BufferedImage(destWidth, destHeight, BufferedImage.TYPE_INT_RGB);
            Graphics g = tag.getGraphics();
            g.drawImage(img, 0, 0, null); // ??
            g.dispose();

            String ext = FilenameUtils.getExtension(srcImageFile);

            //path += HEADER_PIC;
            //User loginUser = SystemUtil.getLoginUser(request.getSession());
            //String fileName = user.getOrganizationList().get(0).getId()+"";
            File destImageFile = new File(path + "/" + System.currentTimeMillis() + ".jpg");
            // 
            ImageIO.write(tag, ext, destImageFile);

            //loginUser.setPicPath(SystemConst.SYSTEM_CONTEXT_PATH_VALUE + HEADER_PIC + "/" + fileName);
            //userService.update(loginUser);
            // 
            srcFile.delete();
            return new Response(new ResponseHeader(200, 20));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.piaoyou.util.ImageUtil.java

/**
 * This method returns a fit-sized image for a source image,
 * this method retains the ratio of the source image
 *//*from   w  w w .j a va2 s .  c o  m*/
public static Image getFitSizeImage(Image srcImage, int fitWidth, int fitHeight) throws IOException {
    if ((fitWidth < 100) || (fitHeight < 100))
        throw new IllegalArgumentException("Cannot accept values < 100");

    int srcWidth = srcImage.getWidth(null);//xem lai cho nay vi neu dung BufferedImage thi khong can null
    int srcHeight = srcImage.getHeight(null);
    //log.trace("src w = " + srcWidth + " h = " + srcHeight);

    // don't need any transforms
    if ((srcWidth == fitWidth) && (srcHeight == fitHeight))
        return srcImage;

    int newWidth = srcWidth;
    int newHeight = srcHeight;

    double fitRatio = (double) fitWidth / fitHeight;
    double srcRatio = (double) srcWidth / srcHeight;
    if (srcRatio > fitRatio) {// must cut the width of the source image
        newWidth = (int) (srcHeight * fitRatio);
    } else {// must cut the height of the source image
        newHeight = (int) (srcWidth / fitRatio);
    }
    //log.trace("new w = " + newWidth + " h = " + newHeight);

    ImageFilter cropFilter = new CropImageFilter((srcWidth - newWidth) / 2, (srcHeight - newHeight) / 2,
            newWidth, newHeight);
    ImageProducer cropProd = new FilteredImageSource(srcImage.getSource(), cropFilter);
    Image cropImage = Toolkit.getDefaultToolkit().createImage(cropProd);

    Image retImage = new ImageIcon(getScaledInstance(cropImage, fitWidth, fitHeight)).getImage();

    return retImage;
}

From source file:org.exist.xquery.modules.image.CropFunction.java

/**
 * evaluate the call to the xquery crop() function,
 * it is really the main entry point of this class
 * /*from   w ww . j a v  a  2s . c om*/
 * @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;

    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();
        }
    }

    //get the mime-type
    String mimeType = args[2].itemAt(0).getStringValue();
    String formatName = mimeType.substring(mimeType.indexOf("/") + 1);

    //TODO currently ONLY tested for JPEG!!!
    Image image = null;
    BufferedImage bImage = null;
    try {
        //get the image data
        image = ImageIO.read(((BinaryValue) args[0].itemAt(0)).getInputStream());
        //         image = ImageModule.getImage((Base64BinaryValueType)args[0].itemAt(0));
        //               image = ImageIO.read(new ByteArrayInputStream(getImageData((Base64BinaryValueType)args[0].itemAt(0))));
        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, x2, y2)));
        if (cropImage instanceof BufferedImage) {
            // just in case cropImage is allready an BufferedImage
            bImage = (BufferedImage) cropImage;
        } else {
            bImage = new BufferedImage(cropImage.getHeight(null), cropImage.getWidth(null),
                    BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bImage.createGraphics(); // Paint the image onto the buffered image
            g.drawImage(cropImage, 0, 0, null);
            g.dispose();
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(bImage, formatName, os);

        //return the new croped image data
        return BinaryValueFromInputStream.getInstance(context, new Base64BinaryValueType(),
                new ByteArrayInputStream(os.toByteArray()));

    } catch (Exception e) {
        throw new XPathException(this, e.getMessage());
    }
}