Java tutorial
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.logging.Level; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import net.sourceforge.tess4j.ITesseract; import net.sourceforge.tess4j.Tesseract; import net.sourceforge.tess4j.TesseractException; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.core.Size; import org.opencv.highgui.Highgui; import static org.opencv.highgui.Highgui.IMREAD_GRAYSCALE; import static org.opencv.highgui.Highgui.imread; import org.opencv.imgproc.Imgproc; import static org.opencv.imgproc.Imgproc.ADAPTIVE_THRESH_MEAN_C; import static org.opencv.imgproc.Imgproc.THRESH_BINARY; import static org.opencv.imgproc.Imgproc.adaptiveThreshold; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author thanh */ public class frmMain extends javax.swing.JFrame { /** * Creates new form frmMain */ public frmMain() { initComponents(); File imageFile = new File("D:\\1.jpg"); // System.loadLibrary("opencv_java2412"); //Opencv ti?n x l Mat gray = Highgui.imread(imageFile.getAbsolutePath(), IMREAD_GRAYSCALE); Mat result_threholds = gray.clone(); adaptiveThreshold(gray, result_threholds, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, 40); frmMain.showResult(result_threholds); //Tch OCR ITesseract instance = new Tesseract(); // JNA Interface Mapping // ITesseract instance = new Tesseract1(); // JNA Direct Mapping // instance.setDatapath("<parentPath>"); // replace <parentPath> with path to parent directory of tessdata instance.setLanguage("eng"); try { String result = instance.doOCR(imageFile); jTextArea1.setText(result); } catch (TesseractException e) { System.err.println(e.getMessage()); } } public static BufferedImage mat2Img(Mat in) { BufferedImage out; byte[] data = new byte[in.width() * in.height() * (int) in.elemSize()]; int type; in.get(0, 0, data); if (in.channels() == 1) type = BufferedImage.TYPE_BYTE_GRAY; else type = BufferedImage.TYPE_3BYTE_BGR; out = new BufferedImage(in.width(), in.height(), type); out.getRaster().setDataElements(0, 0, in.width(), in.height(), data); return out; } public static void showResult(Mat img) { Imgproc.resize(img, img, new Size(640, 480)); MatOfByte matOfByte = new MatOfByte(); Highgui.imencode(".jpg", img, matOfByte); byte[] byteArray = matOfByte.toArray(); BufferedImage bufImage = null; try { InputStream in = new ByteArrayInputStream(byteArray); bufImage = ImageIO.read(in); JFrame frame = new JFrame(); frame.getContentPane().add(new JLabel(new ImageIcon(bufImage))); frame.pack(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jTextArea1.setColumns(20); jTextArea1.setRows(5); jScrollPane1.setViewportView(jTextArea1); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 370, Short.MAX_VALUE) .addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 268, Short.MAX_VALUE) .addContainerGap())); pack(); }// </editor-fold>//GEN-END:initComponents /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(frmMain.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new frmMain().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTextArea jTextArea1; // End of variables declaration//GEN-END:variables }