001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.servlet.taglib.ui;
016    
017    import com.liferay.portal.kernel.exception.ImageResolutionException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.image.ImageBag;
020    import com.liferay.portal.kernel.image.ImageToolUtil;
021    import com.liferay.portal.kernel.json.JSONFactoryUtil;
022    import com.liferay.portal.kernel.json.JSONObject;
023    
024    import java.awt.image.RenderedImage;
025    
026    import java.io.IOException;
027    
028    /**
029     * @author Roberto D??az
030     */
031    public class ImageSelectorProcessor {
032    
033            public ImageSelectorProcessor(byte[] bytes) {
034                    _bytes = bytes;
035            }
036    
037            public byte[] cropImage(String cropRegion)
038                    throws IOException, PortalException {
039    
040                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(cropRegion);
041    
042                    int height = jsonObject.getInt("height");
043                    int width = jsonObject.getInt("width");
044                    int x = jsonObject.getInt("x");
045                    int y = jsonObject.getInt("y");
046    
047                    if ((x > 0) || (y > 0) || (width > 0) || (height > 0)) {
048                            ImageBag imageBag = ImageToolUtil.read(_bytes);
049    
050                            RenderedImage renderedImage = imageBag.getRenderedImage();
051    
052                            renderedImage = ImageToolUtil.crop(
053                                    renderedImage, height, width, x, y);
054    
055                            return ImageToolUtil.getBytes(renderedImage, imageBag.getType());
056                    }
057    
058                    return _bytes;
059            }
060    
061            public byte[] scaleImage(int width)
062                    throws ImageResolutionException, IOException {
063    
064                    ImageBag imageBag = ImageToolUtil.read(_bytes);
065    
066                    RenderedImage renderedImage = imageBag.getRenderedImage();
067    
068                    renderedImage = ImageToolUtil.scale(renderedImage, width);
069    
070                    return ImageToolUtil.getBytes(renderedImage, imageBag.getType());
071            }
072    
073            private final byte[] _bytes;
074    
075    }