Coverage Report - org.jaudiotagger.tag.ImageHandling
 
Classes in this File Line Coverage Branch Coverage Complexity
ImageHandling
0%
0/44
0%
0/12
2.143
 
 1  
 package org.jaudiotagger.tag;
 2  
 
 3  
 import org.jaudiotagger.tag.datatype.Artwork;
 4  
 import org.jaudiotagger.tag.id3.valuepair.ImageFormats;
 5  
 
 6  
 import javax.imageio.ImageIO;
 7  
 import javax.imageio.ImageWriter;
 8  
 import java.awt.*;
 9  
 import java.awt.geom.AffineTransform;
 10  
 import java.awt.image.BufferedImage;
 11  
 import java.io.ByteArrayOutputStream;
 12  
 import java.io.IOException;
 13  
 import java.util.Iterator;
 14  
 
 15  
 /**
 16  
  * User: paul
 17  
  * Date: 11-Dec-2008
 18  
  */
 19  0
 public class ImageHandling
 20  
 {
 21  
     /**
 22  
      * Resize the image until the total size require to store the image is less than maxsize
 23  
      * @param artwork
 24  
      * @param maxSize
 25  
      * @throws IOException
 26  
      */
 27  
     public static void reduceQuality(Artwork artwork, int maxSize) throws IOException
 28  
     {
 29  0
         while(artwork.getBinaryData().length > maxSize)
 30  
         {
 31  0
             Image srcImage = artwork.getImage();
 32  0
             int w = srcImage.getWidth(null);
 33  0
             int newSize = w /2;
 34  0
             makeSmaller(artwork,newSize);
 35  0
         }
 36  0
     }
 37  
      /**
 38  
      * Resize image using Java 2D
 39  
       * @param artwork
 40  
       * @param size
 41  
       * @throws java.io.IOException
 42  
       */
 43  
     private static void makeSmaller(Artwork artwork,int size) throws IOException
 44  
     {
 45  0
         Image srcImage = artwork.getImage();
 46  
 
 47  0
         int w = srcImage.getWidth(null);
 48  0
         int h = srcImage.getHeight(null);
 49  
 
 50  
         // Determine the scaling required to get desired result.
 51  0
         float scaleW = (float) size / (float) w;
 52  0
         float scaleH = (float) size / (float) h;
 53  
 
 54  
         //Create an image buffer in which to paint on, create as an opaque Rgb type image, it doesnt matter what type
 55  
         //the original image is we want to convert to the best type for displaying on screen regardless
 56  0
         BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
 57  
 
 58  
         // Set the scale.
 59  0
         AffineTransform tx = new AffineTransform();
 60  0
         tx.scale(scaleW, scaleH);
 61  
 
 62  
         // Paint image.
 63  0
         Graphics2D g2d = bi.createGraphics();
 64  0
         g2d.drawImage(srcImage, tx, null);
 65  0
         g2d.dispose();
 66  
 
 67  
 
 68  0
         if(artwork.getMimeType()!=null && isMimeTypeWritable(artwork.getMimeType()))
 69  
         {
 70  0
             artwork.setBinaryData(writeImage(bi,artwork.getMimeType()));
 71  
         }
 72  
         else
 73  
         {
 74  0
             artwork.setBinaryData(writeImageAsPng(bi));
 75  
         }
 76  0
     }
 77  
 
 78  
     public static boolean isMimeTypeWritable(String mimeType)
 79  
     {
 80  0
         Iterator<ImageWriter> writers =  ImageIO.getImageWritersByMIMEType(mimeType);
 81  0
         return writers.hasNext();
 82  
     }
 83  
     /**
 84  
      *  Write buffered image as required format
 85  
      *
 86  
      * @param bi
 87  
      * @param mimeType
 88  
      * @return
 89  
      * @throws IOException
 90  
      */
 91  
     public static byte[] writeImage(BufferedImage bi,String mimeType) throws IOException
 92  
     {
 93  0
         Iterator<ImageWriter> writers =  ImageIO.getImageWritersByMIMEType(mimeType);
 94  0
         if(writers.hasNext())
 95  
         {
 96  0
             ImageWriter writer = writers.next();
 97  0
             ByteArrayOutputStream baos = new ByteArrayOutputStream();
 98  0
             writer.setOutput(ImageIO.createImageOutputStream(baos));
 99  0
             writer.write(bi);
 100  0
             return baos.toByteArray();
 101  
         }
 102  0
         throw new IOException("Cannot write to this mimetype");
 103  
     }
 104  
 
 105  
     /**
 106  
      *
 107  
      * @param bi
 108  
      * @return
 109  
      * @throws IOException
 110  
      */
 111  
     public static byte[] writeImageAsPng(BufferedImage bi) throws IOException
 112  
     {
 113  0
         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 114  0
         ImageIO.write(bi, ImageFormats.MIME_TYPE_PNG,baos);
 115  0
         return baos.toByteArray();
 116  
     }
 117  
 
 118  
     /**
 119  
      * Show read formats
 120  
      *
 121  
      * On Windows supports png/jpeg/bmp/gif
 122  
      */
 123  
     public static void showReadFormats()
 124  
     {
 125  0
          String[] formats = ImageIO.getReaderMIMETypes();
 126  0
         for(String f:formats)
 127  
         {
 128  0
             System.out.println("r"+f);
 129  
         }
 130  0
     }
 131  
 
 132  
     /**
 133  
      * Show write formats
 134  
      *
 135  
      * On Windows supports png/jpeg/bmp
 136  
      */
 137  
     public static void showWriteFormats()
 138  
     {
 139  0
          String[] formats = ImageIO.getWriterMIMETypes();
 140  0
         for(String f:formats)
 141  
         {
 142  0
             System.out.println(f);
 143  
         }
 144  0
     }
 145  
 
 146  
 }