Numerical image recognition in Java - java

I want to recognize the numbers in the Pic1.
I did some work on it and it returns to pic2
Here is my code:
package captchadecproj;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
/*
* #author Mr__Hamid
*/
public class NewClass {
public static void main(String args[]) throws IOException {
int width = 110;
int heigth = 40;
BufferedImage image1 = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
BufferedImage num1 = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
BufferedImage image = null;
File f = null;
try {
f = new File("E:\\Desktop 2\\Captcha Project\\CaptchaDecoder\\captchaDecProj\\167.png");
image = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_ARGB);
image = ImageIO.read(f);
System.out.println("Read!");
} catch (IOException e) {
System.out.println("Error" + e);
}
int[] pixel = null;
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
pixel = image.getRaster().getPixel(x, y, new int[3]);
if (pixel[0] < 30 & pixel[1] > 130 & pixel[2] < 110 & pixel[2] > 60) {
image1.setRGB(x, y, Integer.parseInt("ffffff".trim(), 16));
System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (image.getWidth() * y + x));
} else {
image1.setRGB(x, y, 1);
System.out.println(pixel[0] + " - " + pixel[1] + " - " + pixel[2] + " - " + (image.getWidth() * y + x));
}
}
}
try {
f = new File("D:\\Original.jpg");
ImageIO.write(image, "jpg", f);
f = new File("D:\\black&White.jpg");
ImageIO.write(image1, "jpg", f);
System.out.println("Writed");
} catch (IOException e) {
System.out.println("Error" + e);
}
}
}
I have two questions:
How can I split these numbers?
How can I recognize which one is my number?
For example in the uploaded pic: 7, 1, 6

This is answer for first question, which is how to split numbers.
I recommend you something, is to convert your image to two dimensional array, then all operations will be perform much quicker than when you use BufferedImage.
BufferedImage image = ImageIO.read(new URL("http://i.stack.imgur.com/QaTj5.jpg"));
int startPos = 0, lastValue = 0;
Set<Integer> colours = new HashSet<>();
for (int x = 0; x < image.getWidth(); x++) {
int histValue = 0;
for (int y = 0; y < image.getHeight(); y++) {
colours.add(image.getRGB(x, y) );
if (image.getRGB(x, y) == 0xffffFFFF) {
histValue++;
}
}
if (histValue == 0 && lastValue == 0) {
startPos = x;
} else if (histValue == 0 && lastValue != 0) {
BufferedImage segment = image.getSubimage(startPos, 0, x
- startPos, image.getHeight());
ImageIO.write(segment, "jpg", new File("Segment" + startPos
+ ".jpg"));
}
lastValue = histValue;
}
if (lastValue!=0){
BufferedImage segment = image.getSubimage(startPos, 0, image.getWidth()
- startPos, image.getHeight());
ImageIO.write(segment, "jpg", new File("Segment" + startPos
+ ".jpg"));
}
Now all what you need to do is to find some decent algorithm for ocr.

Related

crop image and merge it back without quality loss

I cropped image into small pieces using .getSubimage()
int width = image.getWidth();
int height = image.getHeight();
int c = 4;
int r = 4;
int pWidth = width / c;
int pHeight = height / r;
int x = 0;
int y = 0;
for (int i = 0; i < c; i++) {
y = 0;
for (int j = 0; j < r; j++) {
if ((r - j) == 1 && ((c - i) == 1)) {
BufferedImage SubImage = image.getSubimage(x, y, width - x, height - y);
File outfile = new File(imageName + "/" + "subPic" + i + " " + j + " " + "jpeg");
ImageIO.write(SubImage, "jpeg", outfile);
y += pHeight;
} else if ((r - j) == 1) {
BufferedImage SubImage = image.getSubimage(x, y, pWidth, height - y);
File outfile = new File(imageName + "/" + "subPic" + i + " " + j + " " + "jpeg");
ImageIO.write(SubImage, "jpeg", outfile);
y += pHeight;
} else if ((c - i) == 1) {
BufferedImage SubImage = image.getSubimage(x, y, width - x, pHeight);
File outfile = new File(imageName + "/" + "subPic" + i + " " + j + " " + "jpeg");
ImageIO.write(SubImage, "jpeg", outfile);
y += pHeight;
} else {
BufferedImage SubImage = image.getSubimage(x, y, pWidth, pHeight);
y += pHeight;
File outfile = new File(imageName + "/" + "subPic" + i + " " + j + " " + "jpeg");
ImageIO.write(SubImage, "jpeg", outfile);
}
}
x += pWidth;
}
and then merged them all back using g2d.drawimage(),
BufferedImage combinedIm = new BufferedImage(275, 183, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2d = combinedIm.createGraphics();
int currWidth = 0;
int currHeight = 0;
int iter = -1;
for (int i = 0; i < x; i++) {
currHeight = 0;
for (int j = 0; j < y; j++) {
iter += 1;
g2d.drawImage(imagePieces[iter], currWidth, currHeight, null);
currHeight += imagePieces[iter].getHeight();
}
currWidth += imagePieces[iter].getWidth();
}
g2d.dispose();
where imagePieces is an array with sub images of main image. But merged image has worse quality so my quality check always return false.
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (mainPic.getRGB(x, y) != subPic.getRGB(x, y)) {
return false;
There is the original image:
and the merged image:
What else can I use to cut and merge image so it will pass equality check, or maybe there are better ways to check if images are the same.
I see that you are compressing the pieces in .jpg, so you need to decode them back in order to obtain rgb data. I suppose that you couldn't pass the test because of the data loss after compressing/decoding pieces.
Try not to compress pieces and store them in .bmp format. Or you can modify your test: replace the strict comparison by the inequality, so you can compensate the aforementioned quality loss, e.g.:
final int max_diff = 3;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
if (Math.abs(mainPic.getRGB(x, y) - subPic.getRGB(x, y)) > max_diff) {
return false;

How to extract Y, Cb and Cr color components?

I need to decompose a given colored picture in three separate pictures, so that each color component (Y, Cb, Cr) is stored in one picture like here.
Maybe has an idea how I could get these three pictures with
separately Y, Cb or Cr color components? With following peace of code I can just read out the file and convert the color model from RGB to YCbCr.
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class SpaceConverter {
static int [] colorComponentsYCbCr = new int[3];
static int [] colorComponentsRGB = new int[3];
public static void getRGBComponents (int color)
{
colorComponentsRGB [0] = (color & 0xff);
colorComponentsRGB [1] = (color & 0x00ff) >> 8;
colorComponentsRGB [2] = (color & 0x0000ff) >> 16;
}
public static void convertYCbCr2RGB(int [] componentsYCbCrToConvert)
{
int Y = componentsYCbCrToConvert [0];
int Cb = componentsYCbCrToConvert [1];
int Cr = componentsYCbCrToConvert [2];
colorComponentsRGB = new int [3];
colorComponentsRGB [0] = (int) (Y + 1.402 * (Cr - 128));
colorComponentsRGB [1] = (int) (Y - 0.34414 * (Cb - 128) - 0.71414 * (Cr - 128));
colorComponentsRGB [2] = (int) (Y + 1.772 * (Cb - 128));
}
public static void convertRGB2YCbCr(int [] componentsRGB)
{
int blue = componentsRGB [0];
int green = componentsRGB [1];
int red = componentsRGB [2];
colorComponentsYCbCr [0] = (int) (0.299 * red + 0.587 * green + 0.114 * blue);
colorComponentsYCbCr [1] = (int) (128-0.169 * red-0.331 * green + 0.500 * blue);
colorComponentsYCbCr [2] = (int) (128+0.500 * red - 0.419 * green - 0.081 * blue);
}
public static void getColoredCrPicture(BufferedImage image)
{
File f = null;
// get width and height
int width = image.getWidth();
int height = image.getHeight();
for (int y = 0; y<height; y++)
{
for (int x = 0; x<width; x++)
{
int color = image.getRGB(x, y);
getRGBComponents(color);
convertRGB2YCbCr(colorComponentsRGB);
int Y = colorComponentsYCbCr[0];
int Cb = colorComponentsYCbCr[1];
int Cr = colorComponentsYCbCr[2];
Y = 0;
Cb = 0;
int p = (Y << 24) | (Cb << 16) | (Cr<<8);
image.setRGB(x, y, p);
}
}
try
{
f = new File("/Users/MAC/Documents/workspace/ColorConverter/src/outputX.jpg");
ImageIO.write(image, "jpg", f);
}
catch(IOException e)
{
System.out.println(e);
}
}
public static void getColoredCbPicture(BufferedImage image)
{
File f = null;
// get width and height
int width = image.getWidth();
int height = image.getHeight();
for (int y = 0; y<height; y++)
{
for (int x = 0; x<width; x++)
{
int color = image.getRGB(x, y);
getRGBComponents(color);
convertRGB2YCbCr(colorComponentsRGB);
int Y = colorComponentsYCbCr[0];
int Cb = colorComponentsYCbCr[1];
int Cr = colorComponentsYCbCr[2];
Y = 0;
Cr = 0;
int p = (Y << 24) | (Cb<< 16) | (Cr <<8);
image.setRGB(x, y, p);
}
}
try
{
f = new File("/Users/MAC/Documents/workspace/ColorConverter/src/outputCb.jpg");
ImageIO.write(image, "jpg", f);
System.out.println("WRITE Status: OK");
}
catch(IOException e)
{
System.out.println(e);
}
}
public static BufferedImage loadPicture()
{
File f = null;
BufferedImage img = null;
// read Image
try
{
f = new File("/Users/MAC/Documents/workspace/ColorConverter/src/VILLA.JPG");
img = ImageIO.read(f);
System.out.println("READ Status: OK");
getColoredCbPicture(img);
}
catch(IOException e)
{
System.out.println(e);
}
return img;
}
public static void main(String[] args)
{
BufferedImage image = null;
loadPicture();
getColoredCbPicture(image);
}
}
It sounds like what you are looking to do is take an RGB image, convert it to YCbCr and display each of the three channels in YCbCr as a separate RGB image.
You already have code that converts from RGB to YCbCr . You will also need code that will do the reverse conversion so you can go from YCbCr to RGB.
You will want to use this same logic, but actually create three Y'CrCb images: (Y, 0, 0), (0, Cb, 0) and (0, 0, Cr). Then convert each of these three images to RGB. These three images will be an RGB representation of each of the three YCbCr channels.

How to check if a character is a modifier in java

I am trying to make an image to text program that converts a 0-255 value to a character that matches it (visually). The important thing here is having all characters the same width so in a text editor the text is square...
basically I want all characters from 32-4000 that have the same width as 'X' so I can use 'Q' but not '|' (this font is mono tho so there is no difference)
I am using the Unifont font.
What I have currently works, but I want to supply a general range of unicode characters that are all the same size (so it looks good in text at the end)
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Color fntC = new Color(255, 255, 255);
Graphics2D g = img.createGraphics();
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
for (int i = 32; i <= 4000/*126*/; i++) { //33 126
if (fm.charWidth((char)i) == fm.charWidth('X') && Character.isLetter(i))
dictionary.add(new letter((char) i, getValue((char) i)));
}
This code works pretty well, but I still get characters like 'ȷ' that has a smaller width in the text editor but is treated as the same width as 'X' according to font metrics (I'm using Courier new since that's the default of Notepad++)
MOAR CODE:
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class MainImageToText {
public static void main(String[] args) {
CharDictionary cd = new CharDictionary(new Font("Unifont", 1, 20));
JFileChooser chooser = new JFileChooser();
/*FileNameExtensionFilter filter = new FileNameExtensionFilter("PNG Images", "png");
chooser.setFileFilter(filter);*/
chooser.setCurrentDirectory(new File(System.getProperty("user.home"), "Desktop"));
int returnVal = chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("\nYou chose to open this file: " + f);
File output = new File(f.getParentFile(), "$output.txt");
saveTextFile(output, ImageToText(f, cd));
}
}
static String ImageToText(File f, CharDictionary cd) {
BufferedImage img = null;
String text = "";
try {
img = ImageIO.read(f);
} catch (IOException e) {
e.printStackTrace();
}
int w = img.getWidth();
int h = img.getHeight();
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
Color col = new Color(img.getRGB(x, y));
int red = col.getRed();
int green = col.getGreen();
int blue = col.getBlue();
int finalAverage = (red + green + blue) / 3;
text += cd.getDictionaryLetter(finalAverage);
System.out.println(x + ", " + y);
}
text += "\n";
}
cd.debugPrintDictionary();
// System.out.println(text);
return text;
}
static void saveTextFile(File f, String s) {
File desktop = new File(System.getProperty("user.home"), "Desktop");
File outputfile = new File(desktop.toString() + "\\" + "file.txt");
try {
System.out.println("creating file...");
PrintWriter out = new PrintWriter(outputfile, "UTF-8");
out.write(s);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Saved to: " + f);
}
}
class letter {
public String c;
public int val;
letter(String ch, int dub) {
c = ch;
val = dub;
}
public char getChar() {
Random rand = new Random();
return c.charAt(rand.nextInt(c.length()));
}
public void addChar(char ch) {
c += ch;
}
public void addChar(String ch) {
c += ch;
}
}
class LetterComparator implements Comparator<letter> {
#Override
public int compare(letter a, letter b) {
return (a.val - b.val); // *100 for more precision
}
}
class CharDictionary {
private Font f;
private List<letter> dictionary = new ArrayList<letter>();
CharDictionary(Font font) {
f = font;
createDictionary();
}
public void createDictionary() {
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Color fntC = new Color(255, 255, 255);
Graphics2D g = img.createGraphics();
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
dictionary.add(new letter(" ", getValue(' ')));
for (int i = 32; i <= 65000; i++) { //33 126
if (fm.charWidth(i) == fm.charWidth('X') && !Character.isWhitespace(i))
dictionary.add(new letter((char) i + "", getValue((char) i)));
}
sort();
compact();
}
public void sort() {
Collections.sort(dictionary, new LetterComparator());
}
public void compact() {
int val;
for (int i = dictionary.size()-1; i > 0 ; i--) {
val = dictionary.get(i).val;
if (val == dictionary.get(i-1).val) {
dictionary.get(i-1).addChar(dictionary.get(i).c);
dictionary.remove(i);
}
}
}
public void debugPrintDictionary() {
for (int i = 0; i < dictionary.size(); i++) {
System.out.println("Char: " + dictionary.get(i).c + " Value: " + dictionary.get(i).val);
}
}
/*public int getIndexofVal(int n) {
return null;
}*/
public char getDictionaryLetter(int val) {
for (int i = 0; i < dictionary.size(); i++) {
int charvalue = dictionary.get(i).val;
if (charvalue >= 255-val) {//inverted here
return dictionary.get(i).getChar();
}
}
return dictionary.get(dictionary.size() - 1).getChar(); /// PROBLEM HERE
}
public int getValue(char c) {
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Color fntC = new Color(255, 255, 255);
Graphics2D g = img.createGraphics();
g.setFont(f);
FontMetrics fm = g.getFontMetrics();
int width = fm.charWidth(c);
int height = fm.getAscent()+ fm.getDescent(); // too big
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g = img.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(fntC);
g.setFont(f);
// g.fill
g.drawString(c + "", 0, fm.getAscent());
int w = img.getWidth();
int h = img.getHeight();
double finalAverage = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
Color col = new Color(img.getRGB(x, y));
int red = col.getRed();
int green = col.getGreen();
int blue = col.getBlue();
finalAverage += (red + green + blue) / 3;
}
}
finalAverage /= w * h;
/*try {
File desktop = new File(System.getProperty("user.home"), "Desktop");
File outputfile = new File(desktop.toString() + "\\letters\\" + (int) c + ".png");
ImageIO.write(img, "png", outputfile);
} catch (IOException e) {
}*/
return (int)finalAverage;
}
}

setRGB cannot be resolved or is not a field

Trying to correct a set of "out of order pixels", listed in a XML file previously parsed in Array (number of column on x assis, number of detectors failed) in TIF image using interpolation of (x-1) and (x+ number of failed detectors)/2.
In Input I have a Tif image, as single layer (PAN, MS1, MS2, MS3, MS4) and the Array. In output I want the new image corrected.
I'm having problems with setRGB in :
new_img.setRGB = ((row + numOfDetectors) + (row - 1)/ 2);
and IOException e, in :
} catch (IOException e) {
Sorry, I'm new in Java language. Thanks ..
class ArrayInterpolatorNew {
public static void main(String[] args) {
String path = "E:/IES_Interpol/Resources/Images/ByPass_PAN_C.tif";
String path1 = "E:/IES_Interpol/Resources/Images/ByPass_PAN_C_interpol.tif";
BufferedImage old_img = null;
try { old_img = ImageIO.read(new File(path));}
catch (Exception e) { e.printStackTrace(); }
// Developer may get all the pixels
int width = old_img.getWidth(null);
int height = old_img.getHeight(null);
BufferedImage new_img = new BufferedImage( old_img.getWidth(),
old_img.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);
Graphics gr = new_img.getGraphics();
gr.drawImage(old_img, 0, 0, null);
gr.dispose();
try {
File file = new File("Resources/Images/ByPass_PAN_C_interpol.tif"); // The file to save to.
String format = "TIF"; // Example: "PNG" or "JPG"
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
ImageIO.write(new_img, "TIF", new File(path1));
//}
//catch (IOException e) { e.printStackTrace(); }
byte pix[] = new byte[1];
int n = new_img.getWidth();
int m = new_img.getHeight();
final int firstRow = 1;
final int numOfDetectors = 1;
//int row = image1.getWidth();
//int col = image1.getHeight();
double[][] matrix = new double[n][m];
double[][] Array = new double[firstRow][numOfDetectors];
//swips the rows
for (int row = 0; row < n; ++row)
{
//swips the columns
for (int col = 0; col < m; ++col)
{
//matrix[row][col] = new_img.getRGB(row, col);
new_img.getRaster().getDataElements(row, col, pix);
matrix[row][col] = pix[0];
}
for(int J = 0; J < firstRow; J++)
{
//if (row != J){
// set a pixel
//int image = image1.getRGB( row, col, pixelValue );
//image.setRGB(x, y, pixelValue);
if (row == J){
System.out.println("x,y: " + row + ", " + J + "num of Detectors: " + numOfDetectors);
new_img.setRGB = ((row + numOfDetectors) + (row - 1)/ 2);
if (row == J && numOfDetectors > 20){
System.out.println("x,y: " + row + ", " + J + "num of Detectors: " + numOfDetectors + "then > 20");
//new_img.setRGB(x, y, pixelValue);
int col = new_img.getHeight();
new_img.getRaster().getDataElements(row, col, pix);
matrix[row][col] = pix[0];
}
}
}
try {
//ImageIO.write(new_img, format, file);
} catch (IOException e) {
e.printStackTrace();
} finally { System.out.println("Images were written succesfully.");}
System.out.println("rest of the code...");
}
} finally {
}
}
}

Error stated that the program cannot find symbol for hexa2 and hexa4

I'm a beginner to Java programming. I have the algorithm but I'm having trouble to code it. I would like to compare 2 similar images at position 2 and 4 of RGB hexadecimal pixel values to check whether one of the images has different pixel values than the other one.
My Algorithm logic:
if(image1.substring2 == image2.substring2) && (image1.substring4 == image2.substring4), pixels are the same.
if(image1.substring2 != image2.substring2) || (image1.substring4 != image2.substring4), pixels are not the same.
if(image1.substring2 != image2.substring2) && (image1.substring4 != image2.substring4), pixels are not the same.
I have the remaining codes here. I tried to separate all the process so it is easy for me to troubleshoot later.
//MAIN
public class getPixelRGB1
{
private static int a;
private static int r;
private static int g;
private static int b;
private static final double bitPerColor = 4.0;
public static void main(String[] args) throws IOException
{
FileInputStream image = null;
FileInputStream image2 = null;
getPixelData1 newPD = new getPixelData1();
try {
BufferedImage img, img2;
File file = new File("img0.jpg");
File file2 = new File("imgstega.jpg");
image = new FileInputStream(file);
image2 = new FileInputStream(file2);
img = ImageIO.read(image);
img2 = ImageIO.read(image2);
int rowcol;
int width = img.getWidth();
int height = img.getHeight();
System.out.println("Image's Width: " + width);
System.out.println("Image's Height: " + height);
int[][] pixelData = new int[width * height][3];
System.out.println("Pixel Data: " + pixelData);
int[] rgb;
int count = 0;
for(int i=0; i<width; i++)
{
for(int j=0; j<height; j++)
{
rgb = newPD.getPixelData(img, i, j);
for(int k = 0; k < rgb.length; k++)
{
pixelData[count][k] = rgb[k];
}
count++;
System.out.println("\nRGB Counts: " + count);
}
}
int width2 = img2.getWidth();
int height2 = img2.getHeight();
System.out.println("Image's Width: " + width2);
System.out.println("Image's Height: " + height2);
int[][] pixelData2 = new int[width2 * height2][3];
int[] rgb2;
int counter = 0;
for(int i=0; i<width2; i++)
{
for(int j=0; j<height2; j++)
{
rgb2 = newPD.getPixelData(img2, i, j);
for(int k = 0; k < rgb2.length; k++)
{
pixelData2[counter][k] = rgb2[k];
}
counter++;
System.out.println("\nRGB2 Counts: " + counter);
}
}
} catch (FileNotFoundException ex) {
Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
image.close();
} catch (IOException ex) {
Logger.getLogger(getPixelRGB1.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
//1ST PROCESS - Get RGB Pixel Values
public class getPixelData1
{
private static final double bitPerColor = 4.0;
public int[] getPixelData(BufferedImage img, int w, int h) throws IOException
{
int argb = img.getRGB(w, h);
int rgb[] = new int[]
{
(argb >> 16) & 0xff, //red
(argb >> 8) & 0xff, //green
(argb ) & 0xff //blue
};
int red = rgb[0];
int green = rgb[1]; //RGB Value in Decimal
int blue = rgb[2];
System.out.println("\nRGBValue in Decimal --> " + "\nRed: " + red + " Green: " + green + " Blue: " + blue);
//Convert each channel RGB to Hexadecimal value
String rHex = Integer.toHexString((int)(red));
String gHex = Integer.toHexString((int)(green));
String bHex = Integer.toHexString((int)(blue));
System.out.println("\nRGBValue in Hexa --> " + "\nRed Green Blue " + rHex + gHex + bHex);
//Check position 2 and 4 of hexa value for any changes
String hexa2, hexa4 = "";
String rgbHexa = rHex + gHex + bHex;
hexa2 = rgbHexa.substring(1,2);
System.out.println("\nString RGB Hexa: " + rgbHexa);
System.out.println("\nSubstring at position 2: " + hexa2);
String hex = String.format("%02X%02X%02X", red, green, blue);
hexa4 = hex.substring(3,4);
System.out.println("\nSubstring at position 4: " + hexa4);
return rgb;
}
}
//2nd Process - to compare the RGB Hex value of both images
public class compareHexaRGB
{
public int[] compareHexaRGB(BufferedImage img, BufferedImage img2, int w, int h) throws IOException
{
getPixelData1 newPD = new getPixelData1(); //get method from class getPixelData1 - is this correct?
if((img.hexa2.equals(img2.hexa2)) && (img.hexa4.equals(img2.hexa4)))
{
System.out.println("Pixel values at position 2 and 4 are the same.");
}
else if((img.hexa2 != img2.hexa2) || (img.hexa4 != img2.hexa4))
{
System.out.println("Pixel values at position 2 and 4 are not the same.");
}
else if((img.hexa2 != img2.hexa2) && (img.hexa4 != img2.hexa4))
{
System.out.println("Pixel values at position 2 and 4 are not the same.");
}
}
}
The error stated that the program cannot find symbol for hexa2 and hexa4 in bufferedImage. Can someone check whether I've done something wrong with my coding here? I'm still new to Java.
img is of type BufferedImage (javadoc). It does not contain any non-private (nor private) fields named hexa2 or hexa4.
What you need to do is to refactor your code to make sure you have access to them in compareHexaRGB(). There are probably many ways this can be done. Perhaps you could extend BufferedImage to include your fields, or perhaps you could just pass them as input to the method.
Which solution that would be the more elegant one is hard to determine given that we don't really have all your code (for example, I don't see compareHexaRGB() being called at all).
To be more precise about the compilation problem: By using img.hexa2 to access a field, you assume that there is a field called hexa2 in BufferedImage that is accessible from your class. This is true if a field for example is declared as public. More typically, the fields are private scoped and need to be accessed by a getter/setter. For BufferedImage, no such field exists at all.
Learn about access control here.

Categories