| 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 | |
|
| 17 | |
|
| 18 | |
|
| 19 | 0 | public class ImageHandling |
| 20 | |
{ |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 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 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 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 | |
|
| 51 | 0 | float scaleW = (float) size / (float) w; |
| 52 | 0 | float scaleH = (float) size / (float) h; |
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | 0 | BufferedImage bi = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); |
| 57 | |
|
| 58 | |
|
| 59 | 0 | AffineTransform tx = new AffineTransform(); |
| 60 | 0 | tx.scale(scaleW, scaleH); |
| 61 | |
|
| 62 | |
|
| 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 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 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 | |
|
| 108 | |
|
| 109 | |
|
| 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 | |
|
| 120 | |
|
| 121 | |
|
| 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 | |
|
| 134 | |
|
| 135 | |
|
| 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 | |
} |