Search in sources :

Example 1 with ResampleOp

use of com.mortennobel.imagescaling.ResampleOp in project cerberus-source by cerberustesting.

the class ReadApplicationObjectImage method processRequest.

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 * @throws CerberusException
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, CerberusException {
    String charset = request.getCharacterEncoding();
    String application = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("application"), "", charset);
    String object = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("object"), "", charset);
    int width = (!StringUtils.isEmpty(request.getParameter("w"))) ? Integer.valueOf(request.getParameter("w")) : -1;
    int height = (!StringUtils.isEmpty(request.getParameter("h"))) ? Integer.valueOf(request.getParameter("h")) : -1;
    ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IApplicationObjectService applicationObjectService = appContext.getBean(IApplicationObjectService.class);
    BufferedImage image = applicationObjectService.readImageByKey(application, object);
    BufferedImage b;
    if (image != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        /**
         * If width and height not defined, get image in real size
         */
        if (width == -1 && height == -1) {
            b = image;
        } else {
            ResampleOp rop = new ResampleOp(DimensionConstrain.createMaxDimension(width, height, true));
            rop.setNumberOfThreads(4);
            b = rop.filter(image, null);
        }
        ImageIO.write(b, "png", baos);
    // byte[] bytesOut = baos.toByteArray();
    } else {
        // create a buffered image
        ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
        b = ImageIO.read(bis);
        bis.close();
    }
    SimpleDateFormat sdf = new SimpleDateFormat();
    sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
    sdf.applyPattern("dd MMM yyyy HH:mm:ss z");
    response.setHeader("Last-Modified", sdf.format(DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360)));
    response.setHeader("Expires", sdf.format(DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360)));
    ImageIO.write(b, "png", response.getOutputStream());
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ByteArrayInputStream(java.io.ByteArrayInputStream) SimpleTimeZone(java.util.SimpleTimeZone) ResampleOp(com.mortennobel.imagescaling.ResampleOp) IApplicationObjectService(org.cerberus.crud.service.IApplicationObjectService) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleDateFormat(java.text.SimpleDateFormat) BufferedImage(java.awt.image.BufferedImage)

Example 2 with ResampleOp

use of com.mortennobel.imagescaling.ResampleOp in project cerberus-source by cerberustesting.

the class ReadTestCaseExecutionMedia method returnImage.

private void returnImage(HttpServletRequest request, HttpServletResponse response, TestCaseExecutionFile tc, String filePath) throws IOException {
    int width = (!StringUtils.isEmpty(request.getParameter("w"))) ? Integer.valueOf(request.getParameter("w")) : 150;
    int height = (!StringUtils.isEmpty(request.getParameter("h"))) ? Integer.valueOf(request.getParameter("h")) : 100;
    Boolean real = request.getParameter("r") != null;
    BufferedImage image = null;
    BufferedImage b = null;
    filePath = StringUtil.addSuffixIfNotAlready(filePath, File.separator);
    File picture = new File(filePath + tc.getFileName());
    LOG.debug("Accessing File : " + picture.getAbsolutePath());
    try {
        if (real) {
            b = ImageIO.read(picture);
            ImageIO.write(b, "png", response.getOutputStream());
        } else {
            image = ImageIO.read(picture);
            // We test if file is too thin or too long. That prevent 500 error in case files are not compatible with resize. In that case, we crop the file.
            if ((image.getHeight() * width / image.getWidth() < 10) || (image.getWidth() * height / image.getHeight() < 15)) {
                LOG.debug("Image is too big of thin. Target Height : " + image.getHeight() * width / image.getWidth() + " Target Width : " + image.getWidth() * height / image.getHeight());
                b = ImageIO.read(picture);
                int minwidth = width;
                if (width > image.getWidth()) {
                    minwidth = image.getWidth();
                }
                int minheight = height;
                if (height > image.getHeight()) {
                    minheight = image.getHeight();
                }
                BufferedImage crop = ((BufferedImage) b).getSubimage(0, 0, minwidth, minheight);
                b = crop;
                response.setHeader("Format-Status", "ERROR");
                response.setHeader("Format-Status-Message", "Image Crop from : " + image.getWidth() + "X" + image.getHeight() + " to : " + minwidth + "X" + minheight);
            } else {
                ResampleOp rop = new ResampleOp(DimensionConstrain.createMaxDimension(width, height, true));
                rop.setNumberOfThreads(4);
                b = rop.filter(image, null);
                response.setHeader("Format-Status", "OK");
            }
        }
    } catch (IOException e) {
    }
    SimpleDateFormat sdf = new SimpleDateFormat();
    sdf.setTimeZone(new SimpleTimeZone(0, "GMT"));
    sdf.applyPattern("dd MMM yyyy HH:mm:ss z");
    response.setHeader("Last-Modified", sdf.format(DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360)));
    response.setHeader("Expires", sdf.format(DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360)));
    response.setHeader("Type", "PNG");
    response.setHeader("Description", tc.getFileDesc());
    ImageIO.write(b, "png", response.getOutputStream());
}
Also used : ResampleOp(com.mortennobel.imagescaling.ResampleOp) IFactoryTestCaseExecutionFile(org.cerberus.crud.factory.IFactoryTestCaseExecutionFile) TestCaseExecutionFile(org.cerberus.crud.entity.TestCaseExecutionFile) SimpleDateFormat(java.text.SimpleDateFormat) BufferedImage(java.awt.image.BufferedImage)

Example 3 with ResampleOp

use of com.mortennobel.imagescaling.ResampleOp in project xm-ms-entity by xm-online.

the class ImageResizeUtil method scale.

private static BufferedImage scale(BufferedImage sourceImage, Integer newHeight, Integer newWidth) {
    ResampleOp resizeOp = new ResampleOp(newWidth, newHeight);
    resizeOp.setUnsharpenMask(UnsharpenMask.Normal);
    return resizeOp.filter(sourceImage, null);
}
Also used : ResampleOp(com.mortennobel.imagescaling.ResampleOp)

Aggregations

ResampleOp (com.mortennobel.imagescaling.ResampleOp)3 BufferedImage (java.awt.image.BufferedImage)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 SimpleTimeZone (java.util.SimpleTimeZone)1 TestCaseExecutionFile (org.cerberus.crud.entity.TestCaseExecutionFile)1 IFactoryTestCaseExecutionFile (org.cerberus.crud.factory.IFactoryTestCaseExecutionFile)1 IApplicationObjectService (org.cerberus.crud.service.IApplicationObjectService)1 ApplicationContext (org.springframework.context.ApplicationContext)1