我正在嘗試將位于 MP3 文件中的嵌入圖像與保存為 JPG 的完全相同的圖像進行比較。如果圖像相同,那么我想執行一些進一步的處理,但是,當我比較 2 個圖像(RGB 比較)時,我總是出錯。我確信這些圖像是相同的,因為我從同一個 MP3 中提取了圖像,以使用以下代碼最初創建 JPG。Artwork aw = tag.getFirstArtwork();ByteArrayInputStream bis = new ByteArrayInputStream(aw.getBinaryData());BufferedImage imgA = ImageIO.read(bis);File outputfile = new File("expectedImage.jpg");ImageIO.write(imgA, "jpg", outputfile);在我運行它以獲取圖像后,我剛剛注釋掉了該部分,現在我有了以下代碼來比較 MP3 嵌入圖像和 JPG提取MP3圖片并調用比較方法try { Artwork aw = tag.getFirstArtwork(); ByteArrayInputStream bis = new ByteArrayInputStream(aw.getBinaryData()); BufferedImage imgA = ImageIO.read(bis); File expectedImageFile = new File("expectedImage.jpg"); BufferedImage imgB = ImageIO.read(expectedImageFile); if(compareImages(imgA, imgB)) { System.out.println("The Images Match."); }else { System.out.println("The Images Do Not Match."); }} catch (IOException e) { e.printStackTrace();}比較圖像在第一次通過循環時比較像素的相等性時,該方法失敗。public static boolean compareImages(BufferedImage imgA, BufferedImage imgB) { // The images must be the same size. if (imgA.getWidth() != imgB.getWidth() || imgA.getHeight() != imgB.getHeight()) { return false; } int width = imgA.getWidth(); int height = imgA.getHeight(); // Loop over every pixel. for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { // Compare the pixels for equality. if (imgA.getRGB(x, y) != imgB.getRGB(x, y)) { return false; } } } return true;}
添加回答
舉報
0/150
提交
取消