Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem with image crop and pixel comparison code

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with image crop and pixel comparison code

    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 line
    data1 = (int[]) grab1.getPixels();
    at which point it crashes
    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


  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with image crop and pixel comparison code

    i figured it out. I perform two Linux 'convert' commands on the original PNG image. this somehow corrupts the data. I have modified my code to crop from the image output after the initial 'convert' and the pixel comparison works ok.

Similar Threads

  1. Trying to show change on Image based on pixel values
    By javaGurl in forum Algorithms & Recursion
    Replies: 1
    Last Post: September 20th, 2011, 03:49 PM
  2. Savings account balance comparison code.
    By Rhyssa6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 10th, 2011, 06:57 AM
  3. table comparison
    By awecode in forum JDBC & Databases
    Replies: 2
    Last Post: October 12th, 2010, 09:37 AM
  4. Need help with array comparison
    By raidcomputer in forum Collections and Generics
    Replies: 4
    Last Post: November 17th, 2009, 12:55 PM
  5. Pixel Alteration in an image/image rotation
    By bguy in forum Java Theory & Questions
    Replies: 3
    Last Post: November 1st, 2009, 09:50 AM

Tags for this Thread