SlideShare a Scribd company logo
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.io.*;
import java.util.*;
import java.awt.image.*;
public class ocr extends JFrame
{
JTextField m_fileNameTF = new JTextField(15);
JFileChooser m_fileChooser = new JFileChooser();
DrawingPanel imagepanel;
private JMenuItem menu;
JavaFilter fJavaFilter;
int mouseX,mouseY,mx,my;
Image img;
int flag=3;
int clear;
int flag3=0;
int flag4=0;
int flag5=0;
seg myseg = new seg();
public static void main(String[] args)
{
ocr window = new ocr();
window.setVisible(true);
}
// constructor
ocr()
{
// set the Text field to Read Only mode
m_fileNameTF.setEditable(false);
// Choose only files, not directories
m_fileChooser.setFileSelectionMode ( JFileChooser.FILES_ONLY);
// Set filter for Java source files.
fJavaFilter = new JavaFilter();
m_fileChooser.setFileFilter (fJavaFilter);
//... create Button and its listeners
JButton openButton = new JButton("Open");
JButton lineButton = new JButton("line segment");
JButton wordButton=new JButton("word segment");
JButton charButton=new JButton("char segment");
JButton clearButton=new JButton("clear");
//setting tool tips for various buttons
openButton.setToolTipText("click here to choose a file");
lineButton.setToolTipText("click here for line segmentation");
wordButton.setToolTipText("click here for word segmentation");
charButton.setToolTipText("click here for char segmentation");
clearButton.setToolTipText("click here to clear the panel");
//adding mouse listener to various buttons
openButton.addActionListener(new OpenAction());
lineButton.addActionListener(new LineAction());
wordButton.addActionListener(new wordAction());
charButton.addActionListener(new charAction());
clearButton.addActionListener(new clearAction());
//... Create contant pane, layout components
JPanel content = new JPanel();
JMenuBar bar=new JMenuBar();
setJMenuBar(bar);
JMenu helpmenu=new JMenu("Help ...Alt+H");
helpmenu.setMnemonic('H');
JMenuItem aboutopen=new JMenuItem("About open. A.");
JMenuItem lineseg=new JMenuItem("Line segmentation L");
aboutopen.setMnemonic('A');
lineseg.setMnemonic('L');
aboutopen.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e )
{
JOptionPane.showMessageDialog(null,"To load an image,click open");
}
}
);
lineseg.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent e )
{
JOptionPane.showMessageDialog(null,"To perform lines");
}
}
);
helpmenu.add(aboutopen);
helpmenu.add(lineseg);
content.setLayout(new BorderLayout());
JPanel buttonpanel=new JPanel();
buttonpanel.add(openButton);
buttonpanel.add(m_fileNameTF);
buttonpanel.add(lineButton);
buttonpanel.add(wordButton);
buttonpanel.add(charButton);
buttonpanel.add(clearButton);
content.add(helpmenu,"North");
content.add(buttonpanel,"South");
bar.add(helpmenu);
// Create JPanel canvas to hold the picture
imagepanel = new DrawingPanel();
//imagepanel.addMouseListener(new MouseClickListener());
// Create JScrollPane to hold the canvas containing the picture
JScrollPane scroller = new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scroller.setPreferredSize(new Dimension(500,300));
scroller.setViewportView(imagepanel);
scroller.setViewportBorder(
BorderFactory.createLineBorder(Color.black));
// Add scroller pane to Panel
content.add(scroller,"Center");
//Set window characteristics
this.setTitle("File Browse and View");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setContentPane(content);
this.pack();
} //ocr contructor
class DrawingPanel extends JPanel
{
int x,y;
public DrawingPanel()
{
setBackground(Color.white);
setPreferredSize(new Dimension(750,950));
MouseClickListener listener= new MouseClickListener();
addMouseListener(listener);
Mouserelease listen=new Mouserelease();
addMouseListener(listen);
}
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
if (img!=null && clear==0)
{
g.drawImage(img, 0, 0, this);
}
if(flag==0 && clear==0)
{ int x = 0;
while(myseg.line[x] != -1)
{
g.drawLine(0,myseg.line[x],img.getWidth(imagepanel),myseg.line[x]);
x++;
}
}
if(flag3==1)
{
for(int x=0;x<17;x++)
{ int y=0;
while(myseg.wordarr[x][y]!=-1)
{
g.drawLine(myseg.wordarr[x][y],myseg.line[2*x],myseg.wordarr[x]
[y],myseg.line[2*x+1]);
y++;
}
}
}
if(flag4==1)
{
int z;
int x = 0;
myseg.line[32]=-1;
while(myseg.line[2*x] != -1)
{
int y = 0;
while(myseg.wordarr[x][2*y] != -1)
{
z = 0;
while(myseg.carr[x][y][2*z] !=-1)
{
//System.out.println(x + " " + y + " " + 2*z + carr[x][y][2*z] + carr[x][y][2*z+1]);
g.drawRect(myseg.carr[x][y][2*z],myseg.line[2*x] + 11,myseg.carr[x][y][2*z+1]-myseg.carr[x]
[y][2*z],
myseg.line[2*x+1]-myseg.line[2*x]-11);
z++;
}
y++;
}
x++;
}
}
if(flag5==1)
{
int z;
int x = 0;
myseg.line[32]=-1;
while(myseg.line[2*x] != -1)
{
int y = 0;
while(myseg.wordarr[x][2*y] != -1)
{
z = 0;
while(myseg.carr[x][y][2*z] !=-1)
{
//System.out.println(x + " " + y + " " + 2*z + carr[x][y][2*z] + carr[x][y][2*z+1]);
g.drawRect(myseg.tarr[x][y][2*z],myseg.line[2*x], myseg.tarr[x][y][2*z+1]-myseg.tarr[x][y]
[2*z],
11);
z++;
}
y++;
}
x++;
}
}
}
public int linesegdriver()
{
int width=img.getWidth(imagepanel);
int height=img.getHeight(imagepanel);
int w=width;
int h=height;
int m = 0;
int hHisto[] = new int[h] ;
HHISTO(0, 0, w, h, hHisto);
m = myseg.lineseg(w, h, hHisto);
return m;
}
public int wordsegdriver(int lineno)
{
// System.out.println("In wordseg function");
int w = img.getWidth(imagepanel);
int h = myseg.line[2*lineno+1] - myseg.line[2*lineno];
int lnb = myseg.line[2*lineno];
int lne = myseg.line[2*lineno+1];
int vHisto[] = new int[w+1] ;
int m = 0;
VHISTO(0, lnb, w, h, vHisto);
m = myseg.wordseg(lineno, w, h, vHisto);
//PixelGrabber pg = new PixelGrabber (img,0,lnb,w,h, pixels,0,w);
// PixelGrabber pg = new PixelGrabber (img,0,lnb,w,lne-lnb, pixels,0,w);
//class wordAction implements ActionListener
return m;
}
public void charsegdriver(int ln, int wn)
{
if(myseg.wordarr[ln][2*wn] ==-1 )
{
myseg.carr[ln][wn][0]=-1;
return;
}
int w=myseg.wordarr[ln][2*wn+1]-myseg.wordarr[ln][2*wn];
int h=myseg.line[ln*2+1]-myseg.line[ln*2];
int hHisto[] = new int[h] ;
int hlinepos=-1;
HHISTO(myseg.wordarr[ln][2*wn], myseg.line[ln*2], w, h, hHisto);
hlinepos = myseg.hline(ln, wn, w, h, hHisto);
//System.out.println("hlinepos "+hlinepos);
flag3=1; flag4=1;flag5=1;
h=myseg.line[ln*2+1]-myseg.line[ln*2]-hlinepos-2;
int vHisto[] = new int[w+1] ;
VHISTO(myseg.wordarr[ln][wn*2], myseg.line[ln*2] +hlinepos+3, w, h, vHisto);
myseg.ccharseg(ln, wn, w, h, vHisto);
h=hlinepos-2;
VHISTO(myseg.wordarr[ln][wn*2], myseg.line[ln*2], w, h, vHisto);
myseg.tcharseg(ln, wn, w, h, vHisto);
//lcharseg(vHisto);
}//char
public void sethLine( )
{
repaint();
}
public void setvLine(int lineno, int nowords)
{
flag3=1;
repaint();
}
public void setRect(int lineno, int nowords)
{
flag4=1; flag3=0; flag5=1;
repaint();
}
void VHISTO(int x, int y, int w, int h, int vHisto[])
{
int pixels[] = new int [w*h] ;
int pix[][] = new int [h][w] ;
PixelGrabber pg = new PixelGrabber (img,x,y,w,h,pixels,0,w);
try
{
pg.grabPixels();
}
catch(InterruptedException ee)
{
}
for (int t = 0; t <w ; t++)
vHisto[t] = 0;
for( int i=0;i<w*h;i++)
{
int p= pixels[i] ;
int r = 0xff & (p>>16) ;
int g = 0xff & (p>>8);
int b = 0xff & (p);
if(r>=127 && g>=127&& b>=127)
pixels[i]=1;
else
pixels[i]=0;
}
int j= 0;
for(int m=0; m<h;m++)
{
for(int n=0;n<w;n++)
{
pix[m][n] = pixels[j] ;
if (pix[m][n]==0)
vHisto[n]=vHisto[n] + 1 ;
j++ ;
}
}
//for(int m=0;m<w;m++)
// System.out.println("vHisto= "+vHisto[m]);
return;
} //VHISTO
void HHISTO(int x, int y, int w, int h, int hHisto[])
{
int pixels[] = new int [w*h] ;
int pix[][] = new int [h][w] ;
PixelGrabber pg = new PixelGrabber (img,x,y,w,h,pixels,0,w);
try
{
pg.grabPixels();
}
catch(InterruptedException ee)
{
}
for (int t = 0; t <h ; t++)
hHisto[t] = 0;
for( int i=0;i<w*h;i++)
{
int p= pixels[i] ;
int r = 0xff & (p>>16) ;
int g = 0xff & (p>>8);
int b = 0xff & (p);
if(r>=127 && g>=127&& b>=127)
pixels[i]=1;
else
pixels[i]=0;
}
int j= 0;
for(int m=0; m<h;m++)
{
for(int n=0;n<w;n++)
{
pix[m][n] = pixels[j] ;
if (pix[m][n]==0)
hHisto[m]=hHisto[m] + 1 ;
j++ ;
}
}
//for(int m=0;m<h;m++)
// System.out.println("hHisto= "+hHisto[m]);
return;
} //HHISTO
} //drawing panel
class MouseClickListener extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
mouseX=e.getX();
mouseY=e.getY();
mx=mouseX;
my=mouseY;
}
}
class Mouserelease extends MouseAdapter
{
public void mouseReleased(MouseEvent e)
{
imagepanel.repaint();
}
}
// OpenAction
class OpenAction implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
//... Open a file dialog.
clear=0;
int retval = m_fileChooser.showOpenDialog(ocr.this);
if (retval == JFileChooser.APPROVE_OPTION)
{
//... The user selected a file, process it.
File file = m_fileChooser.getSelectedFile();
// store the file path in a string and send to JNI
String fPath = file.getPath();
System.out.println("The Path is:n" + fPath);
//... Update user interface.
img = Toolkit.getDefaultToolkit().getImage(file.getPath());
m_fileNameTF.setText(file.getName());
imagepanel.repaint();
}
}
}
//this tries to build lines
class LineAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int width = img.getWidth(imagepanel);
int height = img.getHeight(imagepanel);
flag=0;
//System.out.println("in line button");
int nolines = imagepanel.linesegdriver();
imagepanel.sethLine();
}
}
class wordAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int nowords = 0;
//System.out.println("in wordseg");
for(int i = 0; i < 17; i++)
{
nowords = imagepanel.wordsegdriver(i);
System.out.println("line" + i + "nowords" + nowords);
imagepanel.setvLine(i, nowords);
}
return;
}
}
class charAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
for(int i = 0; i < 17; i++)
{
int j = 0;
while(myseg.wordarr[i][j*2] != -1)
{
//System.out.println("in charbox");
imagepanel.charsegdriver(i, j);
imagepanel.setRect(i, j);
j++;
}
}
return;
}
}
class clearAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{imagepanel.removeAll();
imagepanel.revalidate();
//System.out.println("in clear button");
clear=1;
flag3=0;
flag=1;
imagepanel.repaint();
}
}
/** Filter to work with JFileChooser to select java file types. **/
class JavaFilter extends javax.swing.filechooser.FileFilter
{
// this function is internally used for the Filtering action
public boolean accept (File f)
{
return f.getName ().toLowerCase ().endsWith (".jpeg")
|| f.getName ().toLowerCase ().endsWith (".jpg")
|| f.getName ().toLowerCase ().endsWith (".gif")
|| f.isDirectory ();
}
// this function is internally used for the Filter Option drop down menu
public String getDescription ()
{
return "*.jpeg, *.gif, *.jpg";
}
}
}

More Related Content

PPT
Mobile Game and Application with J2ME
PPT
Mobile Game and Application with J2ME - Collision Detection
DOC
BingoConsoleApp
PDF
Pemrograman visual
PDF
20180721 code defragment
PDF
The Ring programming language version 1.3 book - Part 52 of 88
PDF
20180310 functional programming
PDF
20181020 advanced higher-order function
Mobile Game and Application with J2ME
Mobile Game and Application with J2ME - Collision Detection
BingoConsoleApp
Pemrograman visual
20180721 code defragment
The Ring programming language version 1.3 book - Part 52 of 88
20180310 functional programming
20181020 advanced higher-order function

What's hot (20)

DOCX
C programs
DOCX
PDF
The Ring programming language version 1.7 book - Part 63 of 196
DOC
Ds 2 cycle
PDF
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
PDF
The Ring programming language version 1.8 book - Part 65 of 202
PPTX
Making Games in JavaScript
PDF
662305 LAB13
PPTX
PDF
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
PDF
The Ring programming language version 1.10 book - Part 70 of 212
PDF
The Ring programming language version 1.8 book - Part 59 of 202
PDF
The Ring programming language version 1.2 book - Part 38 of 84
PPTX
Михаил Матросов, Повседневный С++: boost и STL
KEY
Javasccript MV* frameworks
PDF
A More Flash Like Web?
PDF
The Ring programming language version 1.5.3 book - Part 79 of 184
DOCX
PDF
Coscup2021-rust-toturial
PDF
The Ring programming language version 1.8 book - Part 66 of 202
C programs
The Ring programming language version 1.7 book - Part 63 of 196
Ds 2 cycle
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
The Ring programming language version 1.8 book - Part 65 of 202
Making Games in JavaScript
662305 LAB13
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
The Ring programming language version 1.10 book - Part 70 of 212
The Ring programming language version 1.8 book - Part 59 of 202
The Ring programming language version 1.2 book - Part 38 of 84
Михаил Матросов, Повседневный С++: boost и STL
Javasccript MV* frameworks
A More Flash Like Web?
The Ring programming language version 1.5.3 book - Part 79 of 184
Coscup2021-rust-toturial
The Ring programming language version 1.8 book - Part 66 of 202
Ad

Viewers also liked (8)

PDF
PPTX
MATLAB Based Vehicle Number Plate Identification System using OCR
PPTX
Optical Character Recognition (OCR)
PPT
optical character recognition system
PPTX
Number plate recognition system using matlab.
PPTX
Presentation_OCR
DOCX
Hand Written Character Recognition Using Neural Networks
PDF
State of the Word 2011
MATLAB Based Vehicle Number Plate Identification System using OCR
Optical Character Recognition (OCR)
optical character recognition system
Number plate recognition system using matlab.
Presentation_OCR
Hand Written Character Recognition Using Neural Networks
State of the Word 2011
Ad

Similar to Ocr code (20)

KEY
Proga 0518
KEY
Proga 0608
PDF
need help with code I wrote. This code is a maze gui, and i need hel.pdf
KEY
Proga 090525
DOC
Seg code
PDF
Know more processing
TXT
Play image
KEY
Kwp2 091217
PDF
Sbaw091020
PDF
I am sorry but my major does not cover programming in depth (ICT) an.pdf
DOCX
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
PDF
10CSL67 CG LAB PROGRAM 9
DOC
Java applet handouts
PDF
SaveI need help with this maze gui that I wrote in java, I am tryi.pdf
DOCX
On the tomcat drive in folder cosc210 you will find file named Paint.docx
PDF
Programa expresiones regulares
KEY
Kwp2 100121
KEY
Kwp2 100121
PDF
Computer Graphics in Java and Scala - Part 1b
Proga 0518
Proga 0608
need help with code I wrote. This code is a maze gui, and i need hel.pdf
Proga 090525
Seg code
Know more processing
Play image
Kwp2 091217
Sbaw091020
I am sorry but my major does not cover programming in depth (ICT) an.pdf
asmt7~$sc_210_-_assignment_7_fall_15.docasmt7cosc_210_-_as.docx
10CSL67 CG LAB PROGRAM 9
Java applet handouts
SaveI need help with this maze gui that I wrote in java, I am tryi.pdf
On the tomcat drive in folder cosc210 you will find file named Paint.docx
Programa expresiones regulares
Kwp2 100121
Kwp2 100121
Computer Graphics in Java and Scala - Part 1b

Recently uploaded (20)

PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
additive manufacturing of ss316l using mig welding
PDF
Well-logging-methods_new................
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
composite construction of structures.pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
web development for engineering and engineering
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPT
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
PPTX
Sustainable Sites - Green Building Construction
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
additive manufacturing of ss316l using mig welding
Well-logging-methods_new................
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
composite construction of structures.pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
web development for engineering and engineering
Operating System & Kernel Study Guide-1 - converted.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
CRASH COURSE IN ALTERNATIVE PLUMBING CLASS
Sustainable Sites - Green Building Construction

Ocr code

  • 1. import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.JOptionPane; import java.io.*; import java.util.*; import java.awt.image.*; public class ocr extends JFrame { JTextField m_fileNameTF = new JTextField(15); JFileChooser m_fileChooser = new JFileChooser(); DrawingPanel imagepanel; private JMenuItem menu; JavaFilter fJavaFilter; int mouseX,mouseY,mx,my; Image img; int flag=3; int clear; int flag3=0; int flag4=0; int flag5=0; seg myseg = new seg();
  • 2. public static void main(String[] args) { ocr window = new ocr(); window.setVisible(true); } // constructor ocr() { // set the Text field to Read Only mode m_fileNameTF.setEditable(false); // Choose only files, not directories m_fileChooser.setFileSelectionMode ( JFileChooser.FILES_ONLY); // Set filter for Java source files. fJavaFilter = new JavaFilter(); m_fileChooser.setFileFilter (fJavaFilter); //... create Button and its listeners JButton openButton = new JButton("Open"); JButton lineButton = new JButton("line segment"); JButton wordButton=new JButton("word segment");
  • 3. JButton charButton=new JButton("char segment"); JButton clearButton=new JButton("clear"); //setting tool tips for various buttons openButton.setToolTipText("click here to choose a file"); lineButton.setToolTipText("click here for line segmentation"); wordButton.setToolTipText("click here for word segmentation"); charButton.setToolTipText("click here for char segmentation"); clearButton.setToolTipText("click here to clear the panel"); //adding mouse listener to various buttons openButton.addActionListener(new OpenAction()); lineButton.addActionListener(new LineAction()); wordButton.addActionListener(new wordAction()); charButton.addActionListener(new charAction()); clearButton.addActionListener(new clearAction()); //... Create contant pane, layout components JPanel content = new JPanel(); JMenuBar bar=new JMenuBar(); setJMenuBar(bar); JMenu helpmenu=new JMenu("Help ...Alt+H"); helpmenu.setMnemonic('H'); JMenuItem aboutopen=new JMenuItem("About open. A."); JMenuItem lineseg=new JMenuItem("Line segmentation L"); aboutopen.setMnemonic('A'); lineseg.setMnemonic('L');
  • 4. aboutopen.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e ) { JOptionPane.showMessageDialog(null,"To load an image,click open"); } } ); lineseg.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e ) { JOptionPane.showMessageDialog(null,"To perform lines"); } } ); helpmenu.add(aboutopen); helpmenu.add(lineseg); content.setLayout(new BorderLayout()); JPanel buttonpanel=new JPanel(); buttonpanel.add(openButton);
  • 5. buttonpanel.add(m_fileNameTF); buttonpanel.add(lineButton); buttonpanel.add(wordButton); buttonpanel.add(charButton); buttonpanel.add(clearButton); content.add(helpmenu,"North"); content.add(buttonpanel,"South"); bar.add(helpmenu); // Create JPanel canvas to hold the picture imagepanel = new DrawingPanel(); //imagepanel.addMouseListener(new MouseClickListener()); // Create JScrollPane to hold the canvas containing the picture JScrollPane scroller = new JScrollPane( JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS); scroller.setPreferredSize(new Dimension(500,300)); scroller.setViewportView(imagepanel); scroller.setViewportBorder( BorderFactory.createLineBorder(Color.black)); // Add scroller pane to Panel
  • 6. content.add(scroller,"Center"); //Set window characteristics this.setTitle("File Browse and View"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setContentPane(content); this.pack(); } //ocr contructor class DrawingPanel extends JPanel { int x,y; public DrawingPanel() { setBackground(Color.white); setPreferredSize(new Dimension(750,950)); MouseClickListener listener= new MouseClickListener(); addMouseListener(listener); Mouserelease listen=new Mouserelease(); addMouseListener(listen); }
  • 7. protected void paintComponent(Graphics g) { super.paintComponent(g); if (img!=null && clear==0) { g.drawImage(img, 0, 0, this); } if(flag==0 && clear==0) { int x = 0; while(myseg.line[x] != -1) { g.drawLine(0,myseg.line[x],img.getWidth(imagepanel),myseg.line[x]); x++; } } if(flag3==1) { for(int x=0;x<17;x++) { int y=0; while(myseg.wordarr[x][y]!=-1)
  • 8. { g.drawLine(myseg.wordarr[x][y],myseg.line[2*x],myseg.wordarr[x] [y],myseg.line[2*x+1]); y++; } } } if(flag4==1) { int z; int x = 0; myseg.line[32]=-1; while(myseg.line[2*x] != -1) { int y = 0; while(myseg.wordarr[x][2*y] != -1) { z = 0; while(myseg.carr[x][y][2*z] !=-1) { //System.out.println(x + " " + y + " " + 2*z + carr[x][y][2*z] + carr[x][y][2*z+1]); g.drawRect(myseg.carr[x][y][2*z],myseg.line[2*x] + 11,myseg.carr[x][y][2*z+1]-myseg.carr[x] [y][2*z], myseg.line[2*x+1]-myseg.line[2*x]-11);
  • 9. z++; } y++; } x++; } } if(flag5==1) { int z; int x = 0; myseg.line[32]=-1; while(myseg.line[2*x] != -1) { int y = 0; while(myseg.wordarr[x][2*y] != -1) { z = 0; while(myseg.carr[x][y][2*z] !=-1) { //System.out.println(x + " " + y + " " + 2*z + carr[x][y][2*z] + carr[x][y][2*z+1]); g.drawRect(myseg.tarr[x][y][2*z],myseg.line[2*x], myseg.tarr[x][y][2*z+1]-myseg.tarr[x][y] [2*z], 11); z++; }
  • 10. y++; } x++; } } } public int linesegdriver() { int width=img.getWidth(imagepanel); int height=img.getHeight(imagepanel); int w=width; int h=height; int m = 0; int hHisto[] = new int[h] ; HHISTO(0, 0, w, h, hHisto); m = myseg.lineseg(w, h, hHisto); return m; } public int wordsegdriver(int lineno)
  • 11. { // System.out.println("In wordseg function"); int w = img.getWidth(imagepanel); int h = myseg.line[2*lineno+1] - myseg.line[2*lineno]; int lnb = myseg.line[2*lineno]; int lne = myseg.line[2*lineno+1]; int vHisto[] = new int[w+1] ; int m = 0; VHISTO(0, lnb, w, h, vHisto); m = myseg.wordseg(lineno, w, h, vHisto); //PixelGrabber pg = new PixelGrabber (img,0,lnb,w,h, pixels,0,w); // PixelGrabber pg = new PixelGrabber (img,0,lnb,w,lne-lnb, pixels,0,w); //class wordAction implements ActionListener return m; } public void charsegdriver(int ln, int wn) { if(myseg.wordarr[ln][2*wn] ==-1 ) { myseg.carr[ln][wn][0]=-1; return;
  • 12. } int w=myseg.wordarr[ln][2*wn+1]-myseg.wordarr[ln][2*wn]; int h=myseg.line[ln*2+1]-myseg.line[ln*2]; int hHisto[] = new int[h] ; int hlinepos=-1; HHISTO(myseg.wordarr[ln][2*wn], myseg.line[ln*2], w, h, hHisto); hlinepos = myseg.hline(ln, wn, w, h, hHisto); //System.out.println("hlinepos "+hlinepos); flag3=1; flag4=1;flag5=1; h=myseg.line[ln*2+1]-myseg.line[ln*2]-hlinepos-2; int vHisto[] = new int[w+1] ; VHISTO(myseg.wordarr[ln][wn*2], myseg.line[ln*2] +hlinepos+3, w, h, vHisto); myseg.ccharseg(ln, wn, w, h, vHisto); h=hlinepos-2; VHISTO(myseg.wordarr[ln][wn*2], myseg.line[ln*2], w, h, vHisto);
  • 13. myseg.tcharseg(ln, wn, w, h, vHisto); //lcharseg(vHisto); }//char public void sethLine( ) { repaint(); } public void setvLine(int lineno, int nowords) { flag3=1; repaint(); } public void setRect(int lineno, int nowords) { flag4=1; flag3=0; flag5=1; repaint(); } void VHISTO(int x, int y, int w, int h, int vHisto[]) { int pixels[] = new int [w*h] ; int pix[][] = new int [h][w] ;
  • 14. PixelGrabber pg = new PixelGrabber (img,x,y,w,h,pixels,0,w); try { pg.grabPixels(); } catch(InterruptedException ee) { } for (int t = 0; t <w ; t++) vHisto[t] = 0; for( int i=0;i<w*h;i++) { int p= pixels[i] ; int r = 0xff & (p>>16) ; int g = 0xff & (p>>8); int b = 0xff & (p); if(r>=127 && g>=127&& b>=127) pixels[i]=1; else pixels[i]=0;
  • 15. } int j= 0; for(int m=0; m<h;m++) { for(int n=0;n<w;n++) { pix[m][n] = pixels[j] ; if (pix[m][n]==0) vHisto[n]=vHisto[n] + 1 ; j++ ; } } //for(int m=0;m<w;m++) // System.out.println("vHisto= "+vHisto[m]); return; } //VHISTO void HHISTO(int x, int y, int w, int h, int hHisto[]) { int pixels[] = new int [w*h] ; int pix[][] = new int [h][w] ;
  • 16. PixelGrabber pg = new PixelGrabber (img,x,y,w,h,pixels,0,w); try { pg.grabPixels(); } catch(InterruptedException ee) { } for (int t = 0; t <h ; t++) hHisto[t] = 0; for( int i=0;i<w*h;i++) { int p= pixels[i] ; int r = 0xff & (p>>16) ; int g = 0xff & (p>>8); int b = 0xff & (p); if(r>=127 && g>=127&& b>=127) pixels[i]=1; else pixels[i]=0;
  • 17. } int j= 0; for(int m=0; m<h;m++) { for(int n=0;n<w;n++) { pix[m][n] = pixels[j] ; if (pix[m][n]==0) hHisto[m]=hHisto[m] + 1 ; j++ ; } } //for(int m=0;m<h;m++) // System.out.println("hHisto= "+hHisto[m]); return; } //HHISTO } //drawing panel class MouseClickListener extends MouseAdapter {
  • 18. public void mousePressed(MouseEvent e) { mouseX=e.getX(); mouseY=e.getY(); mx=mouseX; my=mouseY; } } class Mouserelease extends MouseAdapter { public void mouseReleased(MouseEvent e) { imagepanel.repaint(); } } // OpenAction class OpenAction implements ActionListener { public void actionPerformed(ActionEvent ae) {
  • 19. //... Open a file dialog. clear=0; int retval = m_fileChooser.showOpenDialog(ocr.this); if (retval == JFileChooser.APPROVE_OPTION) { //... The user selected a file, process it. File file = m_fileChooser.getSelectedFile(); // store the file path in a string and send to JNI String fPath = file.getPath(); System.out.println("The Path is:n" + fPath); //... Update user interface. img = Toolkit.getDefaultToolkit().getImage(file.getPath()); m_fileNameTF.setText(file.getName()); imagepanel.repaint(); } } } //this tries to build lines class LineAction implements ActionListener { public void actionPerformed(ActionEvent e) { int width = img.getWidth(imagepanel);
  • 20. int height = img.getHeight(imagepanel); flag=0; //System.out.println("in line button"); int nolines = imagepanel.linesegdriver(); imagepanel.sethLine(); } } class wordAction implements ActionListener { public void actionPerformed(ActionEvent e) { int nowords = 0; //System.out.println("in wordseg"); for(int i = 0; i < 17; i++) { nowords = imagepanel.wordsegdriver(i); System.out.println("line" + i + "nowords" + nowords); imagepanel.setvLine(i, nowords); } return; }
  • 21. } class charAction implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i = 0; i < 17; i++) { int j = 0; while(myseg.wordarr[i][j*2] != -1) { //System.out.println("in charbox"); imagepanel.charsegdriver(i, j); imagepanel.setRect(i, j); j++; } } return; } } class clearAction implements ActionListener { public void actionPerformed(ActionEvent e)
  • 22. {imagepanel.removeAll(); imagepanel.revalidate(); //System.out.println("in clear button"); clear=1; flag3=0; flag=1; imagepanel.repaint(); } } /** Filter to work with JFileChooser to select java file types. **/ class JavaFilter extends javax.swing.filechooser.FileFilter { // this function is internally used for the Filtering action public boolean accept (File f) { return f.getName ().toLowerCase ().endsWith (".jpeg") || f.getName ().toLowerCase ().endsWith (".jpg") || f.getName ().toLowerCase ().endsWith (".gif") || f.isDirectory (); } // this function is internally used for the Filter Option drop down menu
  • 23. public String getDescription () { return "*.jpeg, *.gif, *.jpg"; } } }