(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);
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;
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