-1

I was trying to do what was proposed here: Converting Image to Color array

The code that I copied:

BufferedImage bi = ImageIO.read( new File( "image.png" ) );
int[] data = ( (DataBufferInt) bi.getRaster().getDataBuffer() ).getData();
for ( int i = 0 ; i < data.length ; i++ ) {
    Color c = new Color(data[i]);
    // RGB is now accessible as
    c.getRed();
    c.getGreen();
    c.getBlue();
}

I changed "image.png" to "BA 0091.jpg" in my code.

Code:

public class BANCPU{
  public static void main(String[] Ugh){
    BufferedImage bi = ImageIO.read( new File( "BA 0091.jpg" ) );
    int[] data = ( (DataBufferInt) bi.getRaster().getDataBuffer() ).getData();
    for ( int i = 0 ; i < data.length ; i++ ) {
      Color c = new Color(data[i]);
      // RGB is now accessible as
      c.getRed();
      c.getGreen();
      c.getBlue();
    }
  }
}

Errors that I got:

File: C:\Users\Ben\Desktop\prgm clas\java junk\BANCPU.java  [line: 3]
Error: cannot find symbol
  symbol:   class BufferedImage
  location: class BANCPU
File: C:\Users\Ben\Desktop\prgm clas\java junk\BANCPU.java  [line: 3]
Error: cannot find symbol
  symbol:   variable ImageIO
  location: class BANCPU
File: C:\Users\Ben\Desktop\prgm clas\java junk\BANCPU.java  [line: 3]
Error: cannot find symbol
  symbol:   class File
  location: class BANCPU
File: C:\Users\Ben\Desktop\prgm clas\java junk\BANCPU.java  [line: 4]
Error: cannot find symbol
  symbol:   class DataBufferInt
  location: class BANCPU
File: C:\Users\Ben\Desktop\prgm clas\java junk\BANCPU.java  [line: 6]
Error: cannot find symbol
  symbol:   class Color
  location: class BANCPU
File: C:\Users\Ben\Desktop\prgm clas\java junk\BANCPU.java  [line: 6]
Error: cannot find symbol
  symbol:   class Color
  location: class BANCPU

Screenshot

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Bengineer8
  • 11
  • 4

1 Answers1

0

You will need to import a few libraries to make that program work.

import java.awt.image.*;
import java.awt.*;
import java.io.*;
import javax.imageio.*;

Add those lines to the top of your program and it should work.

  • That led to some other problems.... `java.lang.ClassCastException: java.awt.image.DataBufferByte cannot be cast to java.awt.image.DataBufferInt at BANCPU.main(BANCPU.java:9) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)` – Bengineer8 May 09 '21 at 05:58
  • Code: `import java.awt.image.*; import java.awt.*; import java.io.*; import javax.imageio.*; public class BANCPU{ public static void main(String[] Ugh) throws IOException{ byte P[] = new byte[1408]; BufferedImage bi = ImageIO.read( new File( "BA 0091.jpg" ) ); int[] data = ( (DataBufferInt) bi.getRaster().getDataBuffer() ).getData(); //System.out.println(data.length); for ( int i = 0 ; i < data.length ; i++ ) { Color c = new Color(data[i]); // RGB is now accessible as } } }` – Bengineer8 May 09 '21 at 06:00