Java-How to compare image files in binary

Image file comparison

Comparison source file

The material was borrowed from PhotoAC. Original image: Woman with PC woman_1.jpg

Comparison with copied file (True)

A file that is just a copy of the copy source file. Since there is no difference in the binary data, the comparison result is assumed to be True. woman_1_copy.jpg

Slightly edit the comparison source file and compare (False)

A slightly edited version of the source file after copying it. The small black dot on the right side of the face is the edited part. Since it was edited, the comparison result is assumed to be False. woman_1_edit.jpg

Comparison with another file (False)

Since it is a completely different file, the comparison result is assumed to be False. woman_2.jpg

For non-existent files (False)

Specify a file name that does not exist. Assumed to be caught by IOException.

Source code

Image files are stored in the "files" folder on the desktop.

Main.java


package samples.compare;

public class Main {
    public static void main(String...strings) {
        //Comparison source file
        String woman_1 = "C:\\\\Users\\user\\Desktop\\files\\woman_1.jpg ";

        //File to be compared
        String woman_1_copy = "C:\\\\Users\\user\\Desktop\\files\\woman_1_copy.jpg ";
        String woman_1_edit = "C:\\\\Users\\user\\Desktop\\files\\woman_1_edit.jpg ";
        String woman_2 = "C:\\\\Users\\user\\Desktop\\files\\woman_2.jpg ";
        String errer = "C:\\\\Users\\user\\Desktop\\files\\errer.jpg ";

        //Instance generation
        FileCompare fc_copy = new FileCompare(woman_1, woman_1_copy);
        FileCompare fc_edit = new FileCompare(woman_1, woman_1_edit);
        FileCompare fc_2 = new FileCompare(woman_1, woman_2);
        FileCompare fc_errer = new FileCompare(woman_1, errer);

        //Display comparison results
        System.out.println("woman_1 compare to woman_1_copy : " + fc_copy.fileCompare() );
        System.out.println("woman_1 compare to woman_1_edit : " + fc_edit.fileCompare() );
        System.out.println("woman_1 compare to woman_2 : " + fc_2.fileCompare() );
        System.out.println("woman_1 compare to errer : " + fc_errer.fileCompare() );
    }
}

FileCompare.java


package samples.compare;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

public class FileCompare {
    private String source;
    private String destination;

    public FileCompare(String source, String destination) {
        this.source = source ;
        this.destination = destination;
    }

    public boolean fileCompare() {
        try {
            return Arrays.equals(Files.readAllBytes(Paths.get(source)), Files.readAllBytes(Paths.get(destination)));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("Failed to read the file.");
        }
        return false;
    }
}

Execution result

As expected, everything except the copied files is Flase. Also, if you specify a file name that does not exist, an IOException was thrown.

woman_1 compare to woman_1_copy : true
woman_1 compare to woman_1_edit : false
woman_1 compare to woman_2 : false
java.nio.file.NoSuchFileException: C:\Users\user\Desktop\files\errer.jpg
	at sun.nio.fs.WindowsException.translateToIOException(Unknown Source)
	at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
	at sun.nio.fs.WindowsException.rethrowAsIOException(Unknown Source)
	at sun.nio.fs.WindowsFileSystemProvider.newByteChannel(Unknown Source)
	at java.nio.file.Files.newByteChannel(Unknown Source)
	at java.nio.file.Files.newByteChannel(Unknown Source)
	at java.nio.file.Files.readAllBytes(Unknown Source)
	at samples.compare.FileCompare.fileCompare(FileCompare.java:19)
	at samples.compare.Main.main(Main.java:21)
 Failed to read the file.
woman_1 compare to errer : false

reference

Qiita How to determine if the file contents match (Java)

PhotoAC <a href="https://www.photo-ac.com/main/detail/3302344?title=PC%E3%82%92%E6%8C%81%E3%81%A4%E5%A5%B3 % E6% 80% A7 "rel =" noopener noreferrer "target =" _ blank "> Woman with PC <a href="https://www.photo-ac.com/main/detail/3411890?title=PC%E3%82%92%E6%93%8D%E4%BD%9C%E3%81%99 % E3% 82% 8B% E5% A5% B3% E6% 80% A7 "rel =" noopener noreferrer "target =" _ blank "> Woman operating a PC

Recommended Posts

Java-How to compare image files in binary
Read binary files in Java 1
Android-Upload image files to Azure Blob Storage in Java
Read binary files in Java 2
Convert SVG files to PNG files in Java
[Rails] Various ways to write in seed files
How to update pre-built files in docker container
Deployment sample to launch ubuntu image in Kubernetes
How to make an image partially transparent in Processing
Converting TSV files to CSV files (with BOM) in Ruby
Introduce RMagick to convert existing existing image files to another format
How to handle TSV files and CSV files in Ruby
[Rails 6] How to set a background image in Rails [CSS]
How to Syntax Highlight files like Dockerfile.production in Pycharm
[Rails] How to display an image in the view
Compare objects in Swift
Add files to jar files
Introduction to JAR files
To debug in eclipse
Compare Lists in Java