2 dragon li 2011 dragon_li_2011 于 2013.10.28 14:56 提问

jsp applet驱动扫描仪

我利用mmsc写了一个驱动扫描仪的applet,这个applet通过本地的run as -> java applet运行一切正常,点击“扫描”就可以选择扫描仪,点击“scan”就可以通过选择的扫描仪进行扫描。
但是我把这个applet放到jsp后,界面显示一切正常,但是点击“扫描”和“scan”没有反应了。我检查了一下,应该是scanner.select()方法和scanner.acquire()方法没反应了,我不知道是不是找不到jtwain.dll还是什么环境的原因。
我在这个问题上已经停滞了好几天了,希望能有高手替我解答。下面是applet代码。

package test;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JButton;
import javax.xml.transform.Source;

import uk.co.mmscomputing.device.scanner.Scanner;
import uk.co.mmscomputing.device.scanner.ScannerDevice;
import uk.co.mmscomputing.device.scanner.ScannerIOException;
import uk.co.mmscomputing.device.scanner.ScannerIOMetadata;
import uk.co.mmscomputing.device.scanner.ScannerListener;
import uk.co.mmscomputing.device.twain.TwainIdentity;
import uk.co.mmscomputing.device.twain.TwainScanner;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class ScannerTest extends Applet implements ImageObserver, ScannerListener {

public int i = 0;

Scanner scanner = Scanner.getDevice();

private ImagePanel imgpanel = null;

public void init() {

    System.out.println("in");

    this.setLayout(new BorderLayout());
    this.setSize(350, 500);
    imgpanel = new ImagePanel();

    JButton btn = new JButton("扫描");
    JButton scanImage = new JButton("scan");

    scanImage.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println(event.getActionCommand() + "||"
                    + event.getSource() + "MouseClick");

            getImage();
        }

    });

    //btn.setLabel("扫描");
    btn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.out.println(event.getActionCommand() + "||"
                    + event.getSource() + "MouseClick");

            scan();
        }

    });

    Panel panelx2 = new Panel(new GridLayout(1, 2));

    Panel panelx = new Panel(new GridLayout(1, 5));
    Panel panel1 = new Panel(new BorderLayout());

    panelx.add(new Panel());
    panelx.add(btn);
    panelx.add(scanImage);
    panelx.add(new Panel());
    //panel1.add(num, BorderLayout.CENTER);
    panelx.add(new Panel());
    //panel1.add(panelx2, BorderLayout.NORTH);
    //panel1.add(panelx, BorderLayout.SOUTH);
    add(panelx, BorderLayout.CENTER);
    panelx2.add(imgpanel);

    this.setVisible(true);
    this.setBackground(Color.ORANGE);

    System.out.println("done");
    //this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}

public void paint(Graphics g) {
    super.paint(g);
}

public void scan() {
    scanner = Scanner.getDevice(); // get a device and set GUI panel up
    if (scanner != null) {
        /*try {
            add(scanner.getScanGUI());
        } catch (ScannerIOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        scanner.addListener(this);

        if (scanner instanceof TwainScanner) {
            TwainIdentity[] list = ((TwainScanner) scanner).getIdentities();
            for (int i = 0; i < list.length; i++) {
                System.out.println(list[i].toString());
            }
        }*/

            //scanner.select();
            //scanner.select("Integrated Camera TWAIN");
            //scanner.getScanGUI();
            //scanner.isBusy();
            //scanner.acquire();

        if (scanner instanceof TwainScanner) {
            TwainIdentity[] list = ((TwainScanner) scanner).getIdentities();
            for (int i = 0; i < list.length; i++) {
                System.out.println(list[i].toString());
            }
        }

            if (scanner.isBusy() == false) {
                selectDevice();
            }

    }
}

public void selectDevice() {

    try {
        scanner.select();
    } catch (ScannerIOException e1) {
        e1.printStackTrace();
    }

}

public void getImage() {
    try {
        scanner.addListener(this);
        scanner.acquire();
    } catch (ScannerIOException e1) {
        e1.printStackTrace();

    }
}




public void stop() { // execute before System.exit
    if (scanner != null) { // make sure user waits for scanner to finish!
        scanner.waitToExit();
    }
}

public static void main(String[] avs) throws IOException {
    /*FileInputStream fi = new FileInputStream("C:/archive/BH.pdf");
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    int index = 0;
    byte[] tmp = new byte[1024];
    while ((index = fi.read(tmp)) > -1) {
        bo.write(tmp, 0, index);
    }
    ;*/

    new ScannerTest().init();

}

class ImagePanel extends Panel {
    public Image myimg = null;

    public ImagePanel() {
        setLayout(null);
        setSize(320, 240);
    }

    public void setImage(Image img) {
        this.myimg = img;
        repaint();
    }

    public void paint(Graphics g) {
        if (myimg != null) {
            g.drawImage(myimg, 0, 0, this);
        }

    }
}

public void update(ScannerIOMetadata.Type type, ScannerIOMetadata metadata) {
    System.out.println("*********Type:" + type);

    File file = null;
    FileOutputStream out = null;

    if (type.equals(ScannerIOMetadata.ACQUIRED)) {
        BufferedImage image = metadata.getImage();
        System.out.println("Have an image now!");
        try {
            //ImageIO.write(image, "png", new File("C:\\D\\mmsc_image.png"));

            file = new File("C:\\D\\mmsc_image_" + i + ".png");
            if (file.exists()) {
                file.delete();
            }

            i++;

            BufferedImage bi = (BufferedImage) createImage(
                    image.getWidth(), image
                            .getHeight());

            Graphics2D g2 = bi.createGraphics();
            g2.drawImage(image, null, null);

            out = new FileOutputStream(file);

            JPEGImageEncoder encoder = JPEGCodec
                    .createJPEGEncoder(out);
            JPEGEncodeParam param = encoder
                    .getDefaultJPEGEncodeParam(bi);
            param.setQuality(1f, false);
            encoder.setJPEGEncodeParam(param);

            encoder.encode(bi);
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if (type.equals(ScannerIOMetadata.NEGOTIATE)) {
        System.out.println("NEGOTIATE");
        ScannerDevice device = metadata.getDevice();
        try {
            // device.setShowUserInterface(true);
            // device.setShowProgressBar(true);
            // device.setResolution(100);
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else if (type.equals(ScannerIOMetadata.STATECHANGE)) {
        System.out.println("STATECHANGE");
        System.err.println(metadata.getStateStr());
        if (metadata.isFinished()) {
            System.out.println("finish");
            //System.exit(0);
        }
    } else if (type.equals(ScannerIOMetadata.EXCEPTION)) {
        System.out.println("EXCEPTION");
        metadata.getException().printStackTrace();
    }
}

}

下面是我的jsp代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">


codebase = "http://java.sun.com/update/1.6.0/jinstall-6u10-windows-i586.cab#Version=6,0,0,32"
WIDTH = "320" HEIGHT = "240" >




<comment>
<embed
        type = "application/x-java-applet;version=1.6" \
        CODE = "test.ScannerTest" \
        JAVA_CODEBASE = "/Test/applet/" \
        ARCHIVE = "mmsc.jar,uk.co.mmscomputing.device.sane.jar,uk.co.mmscomputing.device.twain.jar" \
        WIDTH = "320" \
        HEIGHT = "240"
    scriptable = false
    pluginspage = "http://java.sun.com/products/plugin/index.html#download">
    <noembed>

        </noembed>
</embed>
</comment>


这里说明一点,我在index.jsp目录下建了一个applet文件夹,将用到的jar包和编译后的test包文件放在applet文件夹里,test包里有ScannerTest.class里面,所以jsp是可以正常显示applet的。但是我不知道是不是这样的结构会有什么问题。
希望大家能帮助我。

1个回答

taopp52
taopp52   2016.05.09 20:16

你的问题 解决了没?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!