Issue
I'm comparing two images which i stored in pic folder inside the project
during compilation time i'm getting IOException
I tried previous all solutions but still it is not working
please help me to fix this error
in this project i'm using servlet
pages
here is my Servlet method
BufferedImage imgA = null;
BufferedImage imgB = null;
String Filepath = new String();
File fileA = new File("pic/image2.jpg");
File fileB = new File("pic/image2.jpg");
for(int i=0 ;i<list.size();i++)
{
Filepath = list.get(i).getImagepath();
try
{
imgA = ImageIO.read(fileA);
imgB = ImageIO.read(fileB);
}
catch (IOException e)
{
System.out.println(e);
}
int width1 = imgA.getWidth();
int width2 = imgB.getWidth();
int height1 = imgA.getHeight();
int height2 = imgB.getHeight();
if ((width1 != width2) || (height1 != height2))
System.out.println("Error: Images dimensions"+
" mismatch");
else
{
long difference = 0;
for (int y = 0; y < height1; y++)
{
for (int x = 0; x < width1; x++)
{
int rgbA = imgA.getRGB(x, y);
int rgbB = imgB.getRGB(x, y);
int redA = (rgbA >> 16) & 0xff;
int greenA = (rgbA >> 8) & 0xff;
int blueA = (rgbA) & 0xff;
int redB = (rgbB >> 16) & 0xff;
int greenB = (rgbB >> 8) & 0xff;
int blueB = (rgbB) & 0xff;
difference += Math.abs(redA - redB);
difference += Math.abs(greenA - greenB);
difference += Math.abs(blueA - blueB);
}
}
double total_pixels = width1 * height1 * 3;
double avg_different_pixels = difference / total_pixels;
double percentage = (avg_different_pixels / 255) * 100;
System.out.println("Difference Percentage-->" +
percentage);
}
}
error console gives
javax.imageio.IIOException: Can't read input file!
Dec 12, 2017 12:26:30 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [services.CompareImage] in context with path [/JavaCvServlet] threw exception
Solution
You should try to get the files like below
File fileA = new File(getServletContext().getRealPath("pic/image2.jpg"));
File fileB = new File(getServletContext().getRealPath("pic/image2.jpg"));
Answered By - Vishnu T S