Java源码示例:com.mortennobel.imagescaling.ResampleOp

示例1
@Override
public void execute(FsService fsService, HttpServletRequest request,
		HttpServletResponse response, ServletContext servletContext)
		throws Exception
{
	String target = request.getParameter("target");
	FsItemEx fsi = super.findItem(fsService, target);
	InputStream is = fsi.openInputStream();
	BufferedImage image = ImageIO.read(is);
	int width = fsService.getServiceConfig().getTmbWidth();
	ResampleOp rop = new ResampleOp(DimensionConstrain.createMaxDimension(
			width, -1));
	rop.setNumberOfThreads(4);
	BufferedImage b = rop.filter(image, null);
	ByteArrayOutputStream baos = new ByteArrayOutputStream();
	ImageIO.write(b, "png", baos);
	byte[] bytesOut = baos.toByteArray();
	is.close();

	response.setHeader("Last-Modified",
			DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360)
					.toGMTString());
	response.setHeader("Expires",
			DateUtils.addDays(Calendar.getInstance().getTime(), 2 * 360)
					.toGMTString());

	ImageIO.write(b, "png", response.getOutputStream());
}
 
示例2
public static BufferedImage scaleImage(BufferedImage imageToScale) {
    ResampleOp resizeOp = new ResampleOp(WIDTH, HEIGHT);
    resizeOp.setFilter(ResampleFilters.getLanczos3Filter());
    return resizeOp.filter(imageToScale, null);
}
 
示例3
@Override
public BufferedImage scale(BufferedImage imageToScale, int dWidth, int dHeight) {
    ResampleOp resizeOp = new ResampleOp(dWidth, dHeight);
    resizeOp.setFilter(filter);
    return resizeOp.filter(imageToScale, null);
}
 
示例4
/**
 * Returns thumbnail {@link BufferedImage} of the specified {@link BufferedImage}.
 * Note that this method can only scale specified {@link BufferedImage} down, it will not scale it up.
 * Also note that resulting {@link BufferedImage} will not have width and height larger than specified maximum, but might be less.
 *
 * @param bufferedImage {@link BufferedImage} to create thumbnail for
 * @param maxSize       maximum thumbnail {@link BufferedImage} width and height
 * @return thumbnail {@link BufferedImage} of the specified {@link BufferedImage}
 */
@NotNull
public static BufferedImage createImageThumbnail ( @NotNull final BufferedImage bufferedImage, final int maxSize )
{
    final BufferedImage preview;
    if ( bufferedImage.getWidth () <= maxSize && bufferedImage.getHeight () <= maxSize )
    {
        // Simply return source image, but make sure it has compatible type
        preview = toCompatibleImage ( bufferedImage );
    }
    else
    {
        // Calculate resulting width and height
        final int width;
        final int height;
        if ( bufferedImage.getWidth () > bufferedImage.getHeight () )
        {
            width = maxSize;
            height = Math.round ( ( float ) maxSize * bufferedImage.getHeight () / bufferedImage.getWidth () );
        }
        else if ( bufferedImage.getWidth () < bufferedImage.getHeight () )
        {
            height = maxSize;
            width = Math.round ( ( float ) maxSize * bufferedImage.getWidth () / bufferedImage.getHeight () );
        }
        else
        {
            width = height = maxSize;
        }

        // Creating scaled down image
        if ( width >= 3 && height >= 3 )
        {
            // Using Java Image Scaling library approach
            final ResampleOp scaleOp = new ResampleOp ( width, height );
            preview = scaleOp.filter ( bufferedImage, createCompatibleImage ( bufferedImage ) );
        }
        else
        {
            // Scaling down a very small image
            preview = createCompatibleImage ( Math.max ( 1, width ), Math.max ( 1, height ), Transparency.TRANSLUCENT );
            final Graphics2D g2d = preview.createGraphics ();
            GraphicsUtils.setupImageQuality ( g2d );
            g2d.drawImage ( bufferedImage, 0, 0, width, height, null );
            g2d.dispose ();
        }
    }
    return preview;
}
 
示例5
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());
    }
 
示例6
/**
     * 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() == null ? "UTF-8" : 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());
    }