diff --git a/src/main/java/musicplayer/PlayerGUI.java b/src/main/java/musicplayer/PlayerGUI.java index d507ddb..656d4e9 100644 --- a/src/main/java/musicplayer/PlayerGUI.java +++ b/src/main/java/musicplayer/PlayerGUI.java @@ -32,6 +32,9 @@ public class PlayerGUI { public PlayerGUI() { DatabaseManager.init(); populateLibrary(new TreeMap<>(Gateway.listAllSongsGroupedByAlbum().get())); + libraryView.setRootVisible(false); + libraryView.setToggleClickCount(1); + libraryView.setCellRenderer(new LibraryTreeCellRenderer()); } private void resetTree() { @@ -39,9 +42,6 @@ public class PlayerGUI { DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode(); DefaultTreeModel treeModel = new DefaultTreeModel(rootNode); libraryView.setModel(treeModel); - libraryView.setRootVisible(false); - libraryView.setToggleClickCount(1); - libraryView.setCellRenderer(new LibraryTreeCellRenderer()); } private void populateLibrary(Map> libraryData) { diff --git a/src/main/java/musicplayer/db/Gateway.java b/src/main/java/musicplayer/db/Gateway.java index a5fc84c..d8f52ec 100644 --- a/src/main/java/musicplayer/db/Gateway.java +++ b/src/main/java/musicplayer/db/Gateway.java @@ -73,7 +73,7 @@ public class Gateway { session.beginTransaction(); Optional albumObj = getOneAlbum(metadata.album); if (!albumObj.isPresent()) { - Album album = new Album(metadata.album); + Album album = new Album(metadata.album, metadata.artwork); albumObj = Optional.of(album); session.save(album); } @@ -87,16 +87,4 @@ public class Gateway { session.getTransaction().commit(); } } - - public static void updateAllAlbumArt() { - try (Session session = DatabaseManager.getInstance().getSession()) { - @SuppressWarnings("unchecked") - List albums = session.createCriteria(Album.class).list(); - session.beginTransaction(); - albums.forEach(Album::refreshAlbumArt); - session.getTransaction().commit(); - } - } - - } diff --git a/src/main/java/musicplayer/model/Album.java b/src/main/java/musicplayer/model/Album.java index 6b1677e..51c1705 100644 --- a/src/main/java/musicplayer/model/Album.java +++ b/src/main/java/musicplayer/model/Album.java @@ -3,9 +3,7 @@ package musicplayer.model; import javax.imageio.ImageIO; import javax.persistence.*; import java.awt.*; -import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Optional; @@ -25,14 +23,18 @@ public class Album implements Comparable { @Lob @Column(name = "art", nullable = true, length = 10000) private byte[] art; - @Transient - private static int imageScaleToSize = 50; protected Album() { } - public Album(String name) { + public Album(String name){ this.name = name; + this.art = null; + } + + public Album(String name, byte[] art) { + this.name = name; + this.art = art; } public String toString() { @@ -52,20 +54,6 @@ public class Album implements Comparable { } } - @Transient - public void refreshAlbumArt() { - Optional artGet = songs.iterator().next().getAlbumArt(); - if (artGet.isPresent()) { - try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) { - ImageIO.write(convertToBufferedImage(artGet.get().getScaledInstance(imageScaleToSize, imageScaleToSize, Image.SCALE_SMOOTH)), "jpg", baos); - baos.flush(); - art = baos.toByteArray(); - } catch (IOException e) { - art = null; - } - } - } - @SuppressWarnings("MethodWithMultipleReturnPoints") @Override public boolean equals(Object o) { @@ -85,17 +73,6 @@ public class Album implements Comparable { return result; } - @Transient - public static BufferedImage convertToBufferedImage(Image image) { - BufferedImage newImage = new BufferedImage( - image.getWidth(null), image.getHeight(null), - BufferedImage.TYPE_INT_ARGB); - Graphics2D g = newImage.createGraphics(); - g.drawImage(image, 0, 0, null); - g.dispose(); - return newImage; - } - public String getName() { return name; } diff --git a/src/main/java/musicplayer/model/ExtractedMetadata.java b/src/main/java/musicplayer/model/ExtractedMetadata.java index 417452d..189586b 100644 --- a/src/main/java/musicplayer/model/ExtractedMetadata.java +++ b/src/main/java/musicplayer/model/ExtractedMetadata.java @@ -3,7 +3,13 @@ package musicplayer.model; import org.jaudiotagger.tag.FieldKey; import org.jaudiotagger.tag.Tag; -import java.io.File; +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Optional; /** * Internal class representing metadata extracted from a music file. @@ -15,6 +21,9 @@ public class ExtractedMetadata { public final String genre; public final String songFile; public final String trackNumber; + public final byte[] artwork; + + static final String albumArtRegex = ".*(Cover|Folder|cover|folder)\\.(jpg|png)"; /** * @param audioTags jaudiotagger tag data. @@ -27,5 +36,41 @@ public class ExtractedMetadata { this.artist = audioTags.getFirst(FieldKey.ARTIST); this.genre = audioTags.getFirst(FieldKey.GENRE); this.songFile = songFile.getAbsolutePath(); + byte[] tmpArt = null; + try { + tmpArt = audioTags.getFirstArtwork().getBinaryData(); + BufferedImage image = ImageIO.read(new ByteArrayInputStream(tmpArt)); + tmpArt = convertImageToBytes(image); + } catch (IOException | NullPointerException e) { + try { + Path dir = songFile.toPath().getParent(); + Optional imageFile = Files.walk(dir).filter(x -> x.toString().matches(albumArtRegex)).findFirst(); + if (imageFile.isPresent()) { + Image actualImage = ImageIO.read(imageFile.get().toFile()); + tmpArt = convertImageToBytes(actualImage); + } + } catch (IOException ignored) {} + } + artwork = tmpArt; + } + + private static byte[] convertImageToBytes(Image image){ + int imageScaleToSize = 50; + try (ByteArrayOutputStream baos = new ByteArrayOutputStream()){ + ImageIO.write(convertToBufferedImage(image.getScaledInstance(imageScaleToSize, imageScaleToSize, Image.SCALE_SMOOTH)), "jpg", baos); + baos.flush(); + return baos.toByteArray(); + } catch (IOException ignored) {} + return null; + } + + public static BufferedImage convertToBufferedImage(Image image) { + BufferedImage newImage = new BufferedImage( + image.getWidth(null), image.getHeight(null), + BufferedImage.TYPE_INT_ARGB); + Graphics2D g = newImage.createGraphics(); + g.drawImage(image, 0, 0, null); + g.dispose(); + return newImage; } }