Operate the scanner in Java (using JNI)

Source: Internet
Author: User

This is a small example of using java to operate the scanner:
Package edu. ctgu. JTwacker;

Import java. awt. BorderLayout;
Import java. awt. Cursor;
Import java. awt. Dimension;
Import java. awt. Graphics;
Import java. awt. Rectangle;
Import java. awt. Toolkit;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. awt. event. WindowAdapter;
Import java. awt. event. javaswevent;
Import java. awt. event. WindowListener;
Import java. awt. image. BufferedImage;
Import java. io. File;
Import java. io. FileInputStream;

Import javax. swing. JButton;
Import javax. swing. JComboBox;
Import javax. swing. JFrame;
Import javax. swing. JPanel;
Import javax. swing. JScrollPane;
Import javax. swing. JToolBar;
Import javax. swing. SwingUtilities;

Import com.sun.image.codec.jpeg. Unzip codec;
Import com.sun.image.codec.jpeg. Unzip imagedecoder;

Import edu. ctgu. twain. JTwain;
/*
This is the frame that displays the scanned image.
*/
Public class JTwacker extends JFrame {

Class panel extends JPanel {

/** Image for the inner class
*/
Protected BufferedImage m1_panelbufferedimage;

/** Pnale to diaply the image
*/
Public Panel (){
// No op
}

/** Sets the bufferedimage into the class
* @ Param bi BufferedImage
*/
Public void setBufferedImage (BufferedImage bi ){
If (bi = null ){
Return;
}
M1_panelbufferedimage = bi;
Dimension d = new Dimension (m1_panelbufferedimage. getWidth (this ),
MJPEGPanelBufferedImage. getHeight (this ));
SetPreferredSize (d );
Revalidate ();
Repaint ();
}

/** Paints the component.
* @ Param g Graphics object used for the painting
*/
Public void paintComponent (Graphics g ){
Super. paintComponent (g );
Dimension d = getSize ();
G. setColor (getBackground ());
G. fillRect (0, 0, d. width, d. height );
If (m1_panelbufferedimage! = Null ){
G. drawImage (m1_panelbufferedimage, 0, 0, this );
}
}
}


Protected multicast panel mJpegPanel;


Protected BufferedImage mBufferedImage;

Protected JComboBox mSourcesCombo;

Protected JToolBar mToolBar;

/** Constructor
*/
Public JTwacker (){
Super ("test ");

Mpanel = new Panel ();
JScrollPane ps = new JScrollPane (mJpegPanel,
JScrollPane. VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane. HORIZONTAL_SCROLLBAR_ALWAYS );
GetContentPane (). add (ps, BorderLayout. CENTER );

WindowListener wndCloser = new WindowAdapter (){
Public void windowClosing (WindowEvent e ){
System. exit (0 );
}
};
AddWindowListener (wndCloser );

MToolBar = new JToolBar ("Twain ");
MToolBar. setFloatable (false );
AddButtons ();
GetContentPane (). add (mToolBar, BorderLayout. NORTH );
SetSize (800,600 );

/* Center the frame */
Dimension screenDim = Toolkit. getdefatooltoolkit (). getScreenSize ();
Rectangle frameDim = getBounds ();
SetLocation (
(ScreenDim. width-frameDim. width)/2,
(ScreenDim. height-frameDim. height)/2
);
SetVisible (true );
}

Protected void addButtons (){

JButton _ AB = new JButton ("scan ");
_ AB. addActionListener (new ActionListener (){
Public void actionreceivmed (ActionEvent e ){
AcquireImage ();
}
});
MToolBar. add (_ AB );
MToolBar. addSeparator ();

If (edu. ctgu. twain. JTwain. getInstance (). isTwainAvailble ()){
String [] twainSources = JTwain. getInstance (). getAvailableSources ();
If (twainSources! = Null ){
MSourcesCombo = new JComboBox (twainSources );
} Else {
MSourcesCombo = new JComboBox ();
MSourcesCombo. addItem ("<none available> ");
}
} Else {
MSourcesCombo = new JComboBox ();
MSourcesCombo. addItem ("<none available> ");
}
MToolBar. add (mSourcesCombo );
}



Protected void acquireImage (){
If (JTwain. getInstance (). isTwainAvailble ()){
If (mSourcesCombo. getItemCount ()> 0 ){
String _ source = (String) mSourcesCombo. getSelectedItem ();
If (_ source! = Null ){
String _ filename = JTwain. getInstance (). acquire (_ source );
System. out. println (_ filename );

If (_ filename! = Null & _ filename. length ()> 0 ){
File fChoosen = new File (_ filename );
// Savetofile (fChoosen );
ShowImage (fChoosen );
} Else {
System. out. println ("Oh, what's wrong! ");
}
} // End if
} // End if
} // End if
}



Protected void showImage (final File file ){
If (file = null |! File. exists ()){
Return;
}
SetCursor (Cursor. getPredefinedCursor (Cursor. WAIT_CURSOR ));

Thread runner = new Thread (){
Public void run (){
Try {
FileInputStream in = new FileInputStream (file );
Required imagedecoder decoder = required codec. createJPEGDecoder (in );
MBufferedImage = decoder. decodeAsBufferedImage ();
In. close ();
SwingUtilities. invokeLater (new Runnable (){
Public void run (){
Reset ();
}
});
}
Catch (Exception ex ){
Ex. printStackTrace ();
}
SetCursor (Cursor. getPredefinedCursor (Cursor. DEFAULT_CURSOR ));
}
};
Runner. start ();
}

// Save the scanned image as a file and then upload it to the server or save it to the database.
Protected void savetofile (final File file ){
Try {
File mfile = new File ("c: \ dd.jpg ");
If (mfile. exists ()){
Mfile. delete ();
} Else {
File. renameTo (mfile );
}

} Catch (Exception e ){
E. printStackTrace ();
// TODO: handle exception
}
}

Protected void reset (){
If (mBufferedImage! = Null ){
MBufferedImage (mBufferedImage );

}
}


Public static void main (String argv []) {
New JTwacker ();
}

}

-------------------------


Package edu. ctgu. twain;

/*
This is the class that calls the dynamic link library.
*/
Public class JTwain {


Private static final JTwain mInstance = new JTwain ();
Protected final String DLL_NAME = "jtwain ";

Private JTwain (){
InitLib ();
}

Public static JTwain getInstance (){
Return mInstance;
}

Public native boolean isTwainAvailble ();

Public native String [] getAvailableSources ();

Public native String acquire ();

Public native String acquire (String sourceName );

Private void initLib (){

Try {


System. loadLibrary (DLL_NAME );
} Catch (Exception e ){
E. printStackTrace ();
}

Finally {
// System. out. println ("Loading:" + DLL_NAME + ". dll ");
}
}
}

Implement jtwain. dll files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.