java如何获取.mp3格式文件内置歌曲封面
发布网友
发布时间:2022-04-22 21:29
我来回答
共1个回答
热心网友
时间:2022-05-05 07:41
封面就是图片,用java的类抓取图片即可
package tool;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.SwingUtilities;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;
public class ImageViewer extends JFrame
{
private static final long serialVersionUID = 1L;
private static final String DOWNLOADPATH = "download/";
JTextPane textPane = new JTextPane ();
LinkedList<String> initString = new LinkedList<String> ();
LinkedList<String> initStyles = new LinkedList<String> ();
LinkedList<String> path = new LinkedList<String> ();
public ImageViewer ()
{
setTitle ("图片预览下载器 v1.0");
setLayout (new BorderLayout ());
setSize (500, 300);
setLocationRelativeTo (null);
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
private void addComponents ()
{
final JTextField urltField = new JTextField ();
JPanel right = new JPanel (new FlowLayout (FlowLayout.RIGHT, 0, 0));
final JButton go = new JButton ("GO");
textPane.setEditable (false);
JScrollPane content = new JScrollPane (textPane);
go.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
String url = urltField.getText ().trim ();
resolveHTML (url, "utf8", "(?i)\\<img[^\\>]*src[\\=\\s\'\"]+([^\\>\'\"]+)[\'\"]?[^\\>]*\\>");
}
});
JPanel up = new JPanel (new BorderLayout ());
up.add (urltField, BorderLayout.CENTER);
right.add (go);
JButton download = new JButton ("DOWNLOAD");
download.addActionListener (new ActionListener ()
{
@Override
public void actionPerformed ( ActionEvent e )
{
downloadImages ();
}
});
right.add (download);
up.add (right, BorderLayout.EAST);
add (up, BorderLayout.NORTH);
add (content, BorderLayout.CENTER);
}
private void downloadImages ()
{
File fp = new File (DOWNLOADPATH);
if (!fp.exists ())
{
fp.mkdir ();
}
for ( int i = 0; i < path.size (); i++ )
{
try
{
String p = path.get (i);
URL url = new URL (p);
HttpURLConnection huc = (HttpURLConnection) url.openConnection ();
huc.setRequestMethod ("GET");
huc.setConnectTimeout (5 * 1000);
InputStream is = huc.getInputStream ();
ByteArrayOutputStream baos = new ByteArrayOutputStream ();
byte[] buffer = new byte[1024];
int len = -1;
while (( len = is.read (buffer) ) != -1)
{
baos.write (buffer, 0, len);
}
baos.flush ();
baos.close ();
is.close ();
huc.disconnect ();
byte[] data = baos.toByteArray ();
String name = p.substring (p.lastIndexOf ("/") + 1, p.length ());
name = name.contains (".") ? name : name + ".jpg";
FileOutputStream fos = new FileOutputStream (new File (DOWNLOADPATH + name));
fos.write (data);
fos.flush ();
fos.close ();
}
catch (Exception e)
{
continue;
}
}
}
private void loadImages ( List<String> initString, List<String> initStyles, List<String> url )
{
try
{
StyledDocument doc = textPane.getStyledDocument ();
doc.remove (0, doc.getLength ());
addStylesToDocument (doc, url, initStyles);
for ( int i = 0; i < initString.size (); i++ )
{
doc.insertString (doc.getLength (), initString.get (i), doc.getStyle (initStyles.get (i)));
}
}
catch (Exception e)
{}
}
protected void addStylesToDocument ( StyledDocument doc, List<String> url, List<String> initStyles )
{
Style def = StyleContext.getDefaultStyleContext ().getStyle (StyleContext.DEFAULT_STYLE);
for ( int i = 0; i < initStyles.size (); i++ )
{
Style s = doc.addStyle (initStyles.get (i), def);
StyleConstants.setAlignment (s, StyleConstants.ALIGN_CENTER);
ImageIcon icon = createImageIcon (url.get (i));
if (icon != null)
{
StyleConstants.setIcon (s, icon);
}
}
}
protected static ImageIcon createImageIcon ( String url )
{
URL imgURL = null;
try
{
imgURL = new URL (url);
if (imgURL != null)
{
return new ImageIcon (imgURL);
}
}
catch (Exception e)
{}
return null;
}
private void resolveHTML ( String spec, String charsetName, String regex )
{
try
{
URL url = new URL (spec);
HttpURLConnection huc = (HttpURLConnection) url.openConnection ();
InputStreamReader isr = new InputStreamReader (huc.getInputStream (), charsetName);
BufferedReader br = new BufferedReader (isr);
StringBuilder builder = new StringBuilder ();
String line = null;
while (null != ( line = br.readLine () ))
{
builder.append (line);
}
br.close ();
isr.close ();
huc.disconnect ();
String bs = builder.toString ();
Pattern pattern = Pattern.compile (regex);
Matcher matcher = pattern.matcher (bs);
initString.clear ();
initStyles.clear ();
path.clear ();
while (matcher.find ())
{
String p = matcher.group (1);
initString.add (" ");
initStyles.add (p);
path.add (p);
}
loadImages (initString, initStyles, path);
}
catch (Exception e)
{
return;
}
}
public static void main ( String[] args )
{
SwingUtilities.invokeLater (new Runnable ()
{
@Override
public void run ()
{
ImageViewer tester = new ImageViewer ();
tester.addComponents ();
tester.setVisible (true);
}
});
}
}
追问哪些变量是要自己的填的,详细解释一下。
怎么使用,我使用不了