FileDocCategorySizeDatePackage
StandardImageHandler.javaAPI DocJaudiotagger 2.0.44653Wed May 04 20:51:10 BST 2011org.jaudiotagger.tag.images

StandardImageHandler

public class StandardImageHandler extends Object implements ImageHandler
Image Handling used when running on standard JVM

Fields Summary
private static StandardImageHandler
instance
Constructors Summary
private StandardImageHandler()


    
Methods Summary
public static org.jaudiotagger.tag.images.StandardImageHandlergetInstanceOf()

        if(instance==null)
        {
            instance = new StandardImageHandler();
        }
        return instance;
    
public booleanisMimeTypeWritable(java.lang.String mimeType)

        Iterator<ImageWriter> writers =  ImageIO.getImageWritersByMIMEType(mimeType);
        return writers.hasNext();
    
public voidmakeSmaller(Artwork artwork, int size)
Resize image using Java 2D

param
artwork
param
size
throws
java.io.IOException

        Image srcImage = (Image)artwork.getImage();

        int w = srcImage.getWidth(null);
        int h = srcImage.getHeight(null);

        // Determine the scaling required to get desired result.
        float scaleW = (float) size / (float) w;
        float scaleH = (float) size / (float) h;

        //Create an image buffer in which to paint on, create as an opaque Rgb type image, it doesnt matter what type
        //the original image is we want to convert to the best type for displaying on screen regardless
        BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);

        // Set the scale.
        AffineTransform tx = new AffineTransform();
        tx.scale(scaleW, scaleH);

        // Paint image.
        Graphics2D g2d = bi.createGraphics();
        g2d.drawImage(srcImage, tx, null);
        g2d.dispose();


        if(artwork.getMimeType()!=null && isMimeTypeWritable(artwork.getMimeType()))
        {
            artwork.setBinaryData(writeImage(bi,artwork.getMimeType()));
        }
        else
        {
            artwork.setBinaryData(writeImageAsPng(bi));
        }
    
public voidreduceQuality(Artwork artwork, int maxSize)
Resize the image until the total size require to store the image is less than maxsize

param
artwork
param
maxSize
throws
IOException

        while(artwork.getBinaryData().length > maxSize)
        {
            Image srcImage = (Image)artwork.getImage();
            int w = srcImage.getWidth(null);
            int newSize = w /2;
            makeSmaller(artwork,newSize);
        }
    
public voidshowReadFormats()
Show read formats On Windows supports png/jpeg/bmp/gif

         String[] formats = ImageIO.getReaderMIMETypes();
        for(String f:formats)
        {
            System.out.println("r"+f);
        }
    
public voidshowWriteFormats()
Show write formats On Windows supports png/jpeg/bmp

         String[] formats = ImageIO.getWriterMIMETypes();
        for(String f:formats)
        {
            System.out.println(f);
        }
    
public byte[]writeImage(java.awt.image.BufferedImage bi, java.lang.String mimeType)
Write buffered image as required format

param
bi
param
mimeType
return
throws
IOException

        Iterator<ImageWriter> writers =  ImageIO.getImageWritersByMIMEType(mimeType);
        if(writers.hasNext())
        {
            ImageWriter writer = writers.next();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            writer.setOutput(ImageIO.createImageOutputStream(baos));
            writer.write(bi);
            return baos.toByteArray();
        }
        throw new IOException("Cannot write to this mimetype");
    
public byte[]writeImageAsPng(java.awt.image.BufferedImage bi)

param
bi
return
throws
IOException

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bi, ImageFormats.MIME_TYPE_PNG,baos);
        return baos.toByteArray();