SlideShare a Scribd company logo
1 of 23
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

What's hot

What's hot (20)

C programs
C programsC programs
C programs
 
No3
No3No3
No3
 
The Ring programming language version 1.7 book - Part 63 of 196
The Ring programming language version 1.7 book - Part 63 of 196The Ring programming language version 1.7 book - Part 63 of 196
The Ring programming language version 1.7 book - Part 63 of 196
 
Ds 2 cycle
Ds 2 cycleDs 2 cycle
Ds 2 cycle
 
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
 
The Ring programming language version 1.8 book - Part 65 of 202
The Ring programming language version 1.8 book - Part 65 of 202The Ring programming language version 1.8 book - Part 65 of 202
The Ring programming language version 1.8 book - Part 65 of 202
 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScript
 
662305 LAB13
662305 LAB13662305 LAB13
662305 LAB13
 
OOXX
OOXXOOXX
OOXX
 
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...Sokoban Game Development Using Java ( Updated using Screenshots & Class Diagr...
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.10 book - Part 70 of 212The Ring programming language version 1.10 book - Part 70 of 212
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.8 book - Part 59 of 202The Ring programming language version 1.8 book - Part 59 of 202
The Ring programming language version 1.8 book - Part 59 of 202
 
The Ring programming language version 1.2 book - Part 38 of 84
The Ring programming language version 1.2 book - Part 38 of 84The Ring programming language version 1.2 book - Part 38 of 84
The Ring programming language version 1.2 book - Part 38 of 84
 
Михаил Матросов, Повседневный С++: boost и STL
Михаил Матросов, Повседневный С++: boost и STLМихаил Матросов, Повседневный С++: boost и STL
Михаил Матросов, Повседневный С++: boost и STL
 
Javasccript MV* frameworks
Javasccript MV* frameworksJavasccript MV* frameworks
Javasccript MV* frameworks
 
A More Flash Like Web?
A More Flash Like Web?A More Flash Like Web?
A More Flash Like Web?
 
The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.5.3 book - Part 79 of 184The Ring programming language version 1.5.3 book - Part 79 of 184
The Ring programming language version 1.5.3 book - Part 79 of 184
 
Ann
AnnAnn
Ann
 
Coscup2021-rust-toturial
Coscup2021-rust-toturialCoscup2021-rust-toturial
Coscup2021-rust-toturial
 
The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202The Ring programming language version 1.8 book - Part 66 of 202
The Ring programming language version 1.8 book - Part 66 of 202
 

Viewers also liked (8)

Thesis
ThesisThesis
Thesis
 
MATLAB Based Vehicle Number Plate Identification System using OCR
MATLAB Based Vehicle Number Plate Identification System using OCRMATLAB Based Vehicle Number Plate Identification System using OCR
MATLAB Based Vehicle Number Plate Identification System using OCR
 
Optical Character Recognition (OCR)
Optical Character Recognition (OCR)Optical Character Recognition (OCR)
Optical Character Recognition (OCR)
 
optical character recognition system
optical character recognition systemoptical character recognition system
optical character recognition system
 
Number plate recognition system using matlab.
Number plate recognition system using matlab.Number plate recognition system using matlab.
Number plate recognition system using matlab.
 
Presentation_OCR
Presentation_OCRPresentation_OCR
Presentation_OCR
 
Hand Written Character Recognition Using Neural Networks
Hand Written Character Recognition Using Neural Networks Hand Written Character Recognition Using Neural Networks
Hand Written Character Recognition Using Neural Networks
 
State of the Word 2011
State of the Word 2011State of the Word 2011
State of the Word 2011
 

Similar to Ocr code

Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
apexcomputer54
 
import java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfimport java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdf
anwarsadath111
 
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfneed help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdf
arcotstarsports
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
info30292
 
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdfimport java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
anyacarpets
 
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdfIntroduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
fathimafancyjeweller
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
rajkumarm401
 

Similar to Ocr code (20)

Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdfImport java.awt.; Import acm.program.; Import acm.graphics.;.pdf
Import java.awt.; Import acm.program.; Import acm.graphics.;.pdf
 
import java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdfimport java.util.Scanner;public class Main {    public static in.pdf
import java.util.Scanner;public class Main {    public static in.pdf
 
PRACTICAL COMPUTING
PRACTICAL COMPUTINGPRACTICAL COMPUTING
PRACTICAL COMPUTING
 
Software Visualization - Promises & Perils
Software Visualization - Promises & PerilsSoftware Visualization - Promises & Perils
Software Visualization - Promises & Perils
 
need help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdfneed help with code I wrote. This code is a maze gui, and i need hel.pdf
need help with code I wrote. This code is a maze gui, and i need hel.pdf
 
public class TrequeT extends AbstractListT { .pdf
  public class TrequeT extends AbstractListT {  .pdf  public class TrequeT extends AbstractListT {  .pdf
public class TrequeT extends AbstractListT { .pdf
 
Python과 node.js기반 데이터 분석 및 가시화
Python과 node.js기반 데이터 분석 및 가시화Python과 node.js기반 데이터 분석 및 가시화
Python과 node.js기반 데이터 분석 및 가시화
 
Write Python for Speed
Write Python for SpeedWrite Python for Speed
Write Python for Speed
 
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdfimport java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
import java.awt.;import java.awt.event.MouseAdaptor;import java.pdf
 
Sbaw090519
Sbaw090519Sbaw090519
Sbaw090519
 
Java Simple Programs
Java Simple ProgramsJava Simple Programs
Java Simple Programs
 
Proga 0622
Proga 0622Proga 0622
Proga 0622
 
662305 11
662305 11662305 11
662305 11
 
Let’s talk about microbenchmarking
Let’s talk about microbenchmarkingLet’s talk about microbenchmarking
Let’s talk about microbenchmarking
 
662305 LAB12
662305 LAB12662305 LAB12
662305 LAB12
 
ES6(ES2015) is beautiful
ES6(ES2015) is beautifulES6(ES2015) is beautiful
ES6(ES2015) is beautiful
 
Aditazz 01-ul
Aditazz 01-ulAditazz 01-ul
Aditazz 01-ul
 
java program assigment -2
java program assigment -2java program assigment -2
java program assigment -2
 
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdfIntroduction to Computer Graphics using OpenGLCan someone tell me .pdf
Introduction to Computer Graphics using OpenGLCan someone tell me .pdf
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdfObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
 

Recently uploaded

Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Kandungan 087776558899
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 

Recently uploaded (20)

Unit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdfUnit 2- Effective stress & Permeability.pdf
Unit 2- Effective stress & Permeability.pdf
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
(INDIRA) Call Girl Bhosari Call Now 8617697112 Bhosari Escorts 24x7
 

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"; } } }