程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> 在Java中操作掃描儀(使用JNI)

在Java中操作掃描儀(使用JNI)

編輯:關於JAVA
這是一個用Java來操作掃描儀的小例子: 
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.WindowEvent; 
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.JPEGCodec; 
import com.sun.image.codec.jpeg.JPEGImageDecoder; 

import edu.ctgu.twain.JTwain; 
/* 
這是顯示掃描圖片的frame 
*/ 
public class JTwacker extends JFrame { 

class JPEGPanel extends JPanel { 

/** Image for the inner class 
*/ 
protected BufferedImage mJPEGPanelBufferedImage; 

/** Pnale to diaply the image 
*/ 
public JPEGPanel() { 
// no op 


/** Sets the bufferedimage into the class 
* @param bi BufferedImage 
*/ 
public void setBufferedImage(BufferedImage bi) { 
if (bi == null) { 
return; 

mJPEGPanelBufferedImage = bi; 
Dimension d = new Dimension(mJPEGPanelBufferedImage.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 (mJPEGPanelBufferedImage != null) { 
g.drawImage(mJPEGPanelBufferedImage, 0, 0, this); 





protected JPEGPanel mJpegPanel; 


protected BufferedImage mBufferedImage; 

protected JComboBox mSourcesCombo; 

protected JToolBar mToolBar; 

/** Constructor 
*/ 
public JTwacker() { 
super("測試"); 

mJpegPanel = new JPEGPanel(); 
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.getDefaultToolkit().getScreenSize(); 
Rectangle frameDim = getBounds(); 
setLocation( 
(screenDim.width - frameDim.width) / 2, 
(screenDim.height - frameDim.height) / 2 
); 
setVisible(true); 


protected void addButtons(){ 

JButton _ab = new JButton("掃描"); 
_ab.addActionListener(new ActionListener() { 
public void actionPerformed(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("哎呀,怎麼出錯了!"); 

} // 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); 
JPEGImageDecoder decoder = JPEGCodec.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(); 


//把掃描得到的圖片保存為文件,然後上傳到服務器或保存到數據庫中 
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) { 
mJpegPanel.setBufferedImage(mBufferedImage); 





public static void main(String argv[]) { 
new JTwacker(); 




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


package edu.ctgu.twain; 

/* 
這是調用動態鏈接庫的類 
*/ 
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"); 




實現jtwain.dll的文件
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved