Java ByteBuffer Size cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop, int sourceLeft, int sourceWidth, int sourceHeight)

Here you can find the source of cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop, int sourceLeft, int sourceWidth, int sourceHeight)

Description

cover

License

Open Source License

Declaration

public static void cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop,
        int sourceLeft, int sourceWidth, int sourceHeight) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;

import javax.imageio.stream.FileImageOutputStream;

public class Main {

    public static void cover(BufferedImage source, File to, Image cover, int width, int height, int sourceTop,
            int sourceLeft, int sourceWidth, int sourceHeight) {
        BufferedImage dest = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Image imageSource = source.getScaledInstance(sourceWidth, sourceHeight, Image.SCALE_SMOOTH);
        Graphics graphics = dest.getGraphics();
        graphics.setColor(new Color(225, 225, 225));
        graphics.fillRect(0, 0, width, height);
        graphics.drawImage(imageSource, sourceTop, sourceLeft, null);
        graphics.drawImage(cover, 0, 0, null);

        String mimeType = "image/jpeg";
        ImageWriter writer = ImageIO.getImageWritersByMIMEType(mimeType).next();
        try {//from  w  ww . j  a va 2  s .  c  o m
            FileImageOutputStream toFs = new FileImageOutputStream(to);
            writer.setOutput(toFs);
            IIOImage iioImage = new IIOImage(dest, null, null);
            writer.write(null, iioImage, createImageWriteParam(writer));
            toFs.flush();
            toFs.close();
        } catch (FileNotFoundException e) {
            System.out.println(e);
        } catch (IOException e) {
            System.out.println(e);
        }
    }

    public static ImageWriteParam createImageWriteParam(ImageWriter writer) {
        ImageWriteParam params = writer.getDefaultWriteParam();
        // JPEGImageWriteParam jpegImageWriteParam = new
        // JPEGImageWriteParam(writer.getLocale());
        // jpegImageWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        // jpegImageWriteParam.setCompressionQuality(0.8F);
        params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        params.setCompressionQuality(0.95F);
        return params;
    }
}

Related

  1. checkBounds(BufferedImage image, int xStart, int yStart, int width, int height)
  2. checkBufferSize(ByteBuffer buf, int elementSize)
  3. chooseAppropriateTileSize(BufferedImage image)
  4. collidesWithImage(BufferedImage image, int x, int y, int width, int height, boolean pixelPerfect)
  5. convolve(final BufferedImage image, float[] elements, int theWidth, int theHeight)
  6. cutAndScaleToRoundSquare(BufferedImage src, int size, int radius)
  7. cutAndScaleToSquare(BufferedImage src, int size)
  8. decreaseBufferCapatity(final ByteBuffer byteBuffer, final int size, final int minSize)
  9. decryptData(final ByteBuffer buffer, final int size, final int key)