用Jaudiotagger提取MP3中的图片

Jaudiotagger是jid3lib的扩展,比jlid3lib强大更多,支持更多格式。它的API是:http://www.jthink.net/jaudiotagger/javadoc/overview-summary.html

 

下面是用Jaudiotagger提取MP3图片,并将图片显示出来的代码:

 

import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;
import org.jaudiotagger.tag.id3.AbstractID3v2Frame;
import org.jaudiotagger.tag.id3.AbstractID3v2Tag;
import org.jaudiotagger.tag.id3.framebody.FrameBodyAPIC;

public class Main {

    public static void main(String args[]) throws IOException, TagException, ReadOnlyFileException, InvalidAudioFrameException {

        String url = "D:\\Paparazzi.mp3";
        File sourceFile = new File(url);
        MP3File mp3file = new MP3File(sourceFile);
        
        AbstractID3v2Tag tag = mp3file.getID3v2Tag();
        AbstractID3v2Frame frame = (AbstractID3v2Frame) tag.getFrame("APIC");
        FrameBodyAPIC body = (FrameBodyAPIC) frame.getBody();
        byte[] imageData = body.getImageData();
        Image img=Toolkit.getDefaultToolkit().createImage(imageData, 0,imageData.length);
        System.out.println("img----" + imageData);
        ImageIcon icon = new ImageIcon(img);            
        FileOutputStream fos = new FileOutputStream("D://test1.jpg");
        fos.write(imageData);
        fos.close();
    
        System.out.println("width:"+icon.getIconWidth());
        System.out.println("height:"+icon.getIconHeight());
        getImg(icon);
    }

    public static void getImg(ImageIcon img){
        JFrame f = new JFrame();
        JLabel l = new JLabel();
        l.setIcon(img);
        l.setVisible(true);
        f.add(l);
        f.setSize(500, 500);    
        f.setVisible(true);
    }

}

你可能感兴趣的:(javaSwing)