java文件快速扫描仪_用Java直接从扫描仪获得扫描数据,然后上载到服务器上,这样的程序需要利用那些知识点(有关Java)?...

这是一个使用Java实现的程序,通过JTwain库直接从扫描仪获取图像,并展示扫描图片。程序包括一个主窗口,用户可以选择扫描仪源,进行扫描操作。扫描后的图片可以显示在窗口中,并能被保存到本地文件,但未提及如何上传到服务器或保存到数据库。
摘要由CSDN通过智能技术生成

展开全部

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

}

} else {

mSourcesCombo = new JComboBox();

mSourcesCombo.addItem("");

}

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

}

//把扫描得到的图片保存32313133353236313431303231363533e58685e5aeb931333238653236为文件,然后上传到服务器或保存到数据库中

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

}

}

}

本回答由提问者推荐

2Q==

c50d6af2748da18e06a2dc6d9597acd0.gif

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值