Example usage for java.awt.image AreaAveragingScaleFilter AreaAveragingScaleFilter

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

Introduction

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

Prototype

public AreaAveragingScaleFilter(int width, int height) 

Source Link

Document

Constructs an AreaAveragingScaleFilter that scales the pixels from its source Image as specified by the width and height parameters.

Usage

From source file:com.sketchy.image.ImageProcessingThread.java

public static BufferedImage resizeImage(BufferedImage image, int drawingWidth, int drawingHeight,
        CenterOption centerOption, ScaleOption scaleOption) {

    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    double widthScale = drawingWidth / (double) imageWidth;
    double heightScale = drawingHeight / (double) imageHeight;
    double scale = Math.min(widthScale, heightScale);

    int scaleWidth = (int) (imageWidth * scale);
    int scaleHeight = (int) (imageHeight * scale);

    BufferedImage resizedImage = new BufferedImage(scaleWidth, scaleHeight, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();

    if (scaleOption == ScaleOption.SCALE_AREA_AVERAGING) {
        ImageProducer prod = new FilteredImageSource(image.getSource(),
                new AreaAveragingScaleFilter(scaleWidth, scaleHeight));
        Image img = Toolkit.getDefaultToolkit().createImage(prod);
        g.drawImage(img, 0, 0, scaleWidth, scaleHeight, null); // it's already scaled
    } else {/*www .  java  2s  .co  m*/
        if (scaleOption == ScaleOption.SCALE_NEAREST_NEIGHBOR) {
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
        } else if (scaleOption == ScaleOption.SCALE_BICUBIC) {
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        } else if (scaleOption == ScaleOption.SCALE_BILINEAR) {
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        }
        g.drawImage(image, 0, 0, scaleWidth, scaleHeight, 0, 0, imageWidth, imageHeight, null);
    }
    g.dispose();

    int cropWidth = (int) Math.min(scaleWidth, drawingWidth);
    int cropHeight = (int) Math.min(scaleHeight, drawingHeight);

    int drawingLeft = 0;
    int drawingTop = 0;
    if ((centerOption == CenterOption.CENTER_HORIZONTAL) || (centerOption == CenterOption.CENTER_BOTH)) {
        drawingLeft = (int) ((drawingWidth - cropWidth) / 2.0);
    }
    if ((centerOption == CenterOption.CENTER_VERTICAL) || (centerOption == CenterOption.CENTER_BOTH)) {
        drawingTop = (int) ((drawingHeight - cropHeight) / 2.0);
    }

    BufferedImage croppedImage = resizedImage.getSubimage(0, 0, cropWidth, cropHeight);
    resizedImage = null;

    BufferedImage drawingImage = new BufferedImage(drawingWidth, drawingHeight, BufferedImage.TYPE_INT_ARGB);
    g = drawingImage.createGraphics();
    g.drawImage(croppedImage, drawingLeft, drawingTop, drawingLeft + cropWidth, drawingTop + cropHeight, 0, 0,
            cropWidth, cropHeight, null);
    g.dispose();

    croppedImage = null;
    return drawingImage;
}

From source file:tv.dyndns.kishibe.qmaclone.server.IconUploadServletStub.java

private void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    logger.log(Level.INFO, request.toString());

    FileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    upload.setSizeMax(64L * 1024L);/*  w ww.ja  v  a  2  s  .  c o  m*/
    List<FileItem> items = null;

    try {
        List<FileItem> temp = upload.parseRequest(request);
        items = temp;
    } catch (FileUploadException e) {
        writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_FAILED_TO_PARSE_REQUEST);
        return;
    }

    String userCode = null;
    BufferedImage inputImage = null;
    long originalFileSize = 0;

    for (FileItem item : items) {
        if (item.isFormField()) {
            String key = item.getFieldName();
            String value = item.getString();

            if (key.equals(Constant.FORM_NAME_USER_CODE)) {
                userCode = value;
            }

            continue;
        }

        // ??
        originalFileSize = item.getSize();
        try (InputStream inputStream = item.getInputStream()) {
            inputImage = ImageIO.read(inputStream);
        } catch (IOException e) {
            writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_FAILED_TO_DETECT_IMAGE_FILE_TYPE);
            return;
        }

        if (inputImage == null) {
            writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_FAILED_TO_DETECT_IMAGE_FILE_TYPE);
            return;
        }
    }

    if (userCode == null || inputImage == null) {
        writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_REQUEST_FORMAT_ERROR);
        return;
    }

    String extension = "jpg";
    String fileTitle = Utility.createFileName();
    String fileName = fileTitle + "." + extension;

    // 
    // TODO(nodchip): ?
    int size = Constant.ICON_SIZE * 2;
    ImageFilter imageFilter = new AreaAveragingScaleFilter(size, size);
    Image middleImage = new Canvas().createImage(new FilteredImageSource(inputImage.getSource(), imageFilter));
    BufferedImage outputImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    outputImage.createGraphics().drawImage(middleImage, 0, 0, size, size, null);

    // ???
    new File(Constant.ICON_FOLDER_PATH).mkdirs();
    File outputFile = new File(Constant.ICON_FOLDER_PATH, fileName);
    ImageIO.write(outputImage, "jpg", outputFile);

    logger.log(Level.INFO, String.format("%d bytes -> %d bytes (%s)", originalFileSize, outputFile.length(),
            outputFile.getPath()));

    // ???
    int userId = Integer.parseInt(userCode);
    PacketUserData userData;
    try {
        userData = database.getUserData(userId);
    } catch (DatabaseException e) {
        logger.log(Level.WARNING, "????????", e);
        throw new ServletException(e);
    }
    File oldFile = new File(Constant.ICON_FOLDER_PATH, userData.imageFileName);
    if (oldFile.isFile() && !oldFile.getName().equals(Constant.ICON_NO_IMAGE)) {
        oldFile.delete();
    }
    userData.imageFileName = fileName;
    try {
        database.setUserData(userData);
    } catch (DatabaseException e) {
        logger.log(Level.WARNING, "???????", e);
        throw new ServletException(e);
    }

    // ????
    writeResponse(response, Constant.ICON_UPLOAD_RESPONSE_OK);
}