I have got some code to read in an image, crop a region from the image and compare with a reference image.
It seems to compile and work ok until the lineat which point it crashesdata1 = (int[]) grab1.getPixels();
when i run the code. I do previously use pdftk and convert (in Ubuntu) to extract .png images from a pdf file so maybe it is a problem with the output .png?
code:
if(NAME.endsWith("_1.1.1.png")) { // // // println("first filename:"); System.out.println(NAME); String curDir = System.getProperty("user.dir"); println("current directory:"); System.out.println(curDir); int flag = 111; System.out.format("flag = %d%n", flag); BufferedImage image = null; try { // Read from a first image file //hardcoded right now! // File file = new File(".../image/2cm.png"); File file = new File(".../s123456TTT2012-06-2_1.1.1.png"); //File file = new File(curDir, '/', NAME); image = ImageIO.read(file); // crop and save output image image = image.getSubimage(100, 40, 100, 100); ImageIO.write(image, "png",new File(".../image/out.png")); //Comp(); } catch (IOException e) { e.printStackTrace(); } // compare the cropped image with the reference image (for 2 cm scale) String file1 = ".../image/ref.png"; String file2 = ".../image/out.png"; Image image1 = Toolkit.getDefaultToolkit().getImage(file1); Image image2 = Toolkit.getDefaultToolkit().getImage(file2); try { PixelGrabber grab1 =new PixelGrabber(image1, 0, 0, -1, -1, false); PixelGrabber grab2 =new PixelGrabber(image2, 0, 0, -1, -1, false); int[] data1 = null; if (grab1.grabPixels()) { int width = grab1.getWidth(); int height = grab1.getHeight(); data1 = new int[width * height]; data1 = (int[]) grab1.getPixels(); } int[] data2 = null; if (grab2.grabPixels()) { int width = grab2.getWidth(); int height = grab2.getHeight(); data2 = new int[width * height]; data2 = (int[]) grab2.getPixels(); } System.out.println("The Calypso data has a 2 cm scale: " + java.util.Arrays.equals(data1, data2)); } catch (InterruptedException e1) { e1.printStackTrace(); }
error:
Now running java PNG to ASCII code... first filename: s123456TTT2012-06-2_1.1.1.png current directory: .../ flag = 111 Exception in thread "Animation Thread" java.lang.ClassCastException: [B cannot be cast to [I at CalypsoAnalysis.setup(CalypsoAnalysis.java:104) at processing.core.PApplet.handleDraw(Unknown Source) at processing.core.PApplet.run(Unknown Source) at java.lang.Thread.run(Thread.java:636)
thanks