简体   繁体   English

Java Applet在浏览器中不起作用-使用图像文件

[英]Java applet doesn't work in the browser - using image file

I have a java applet which demonstrates some filters. 我有一个Java小程序,它演示了一些过滤器。 I have a predefined path for an example image and the user is able to choose an own picture. 我有一个用于示例图像的预定义路径,并且用户可以选择自己的图片。

I can't use new File because I am working in the browser and hav no access to the users storage. 我无法使用新文件,因为我正在浏览器中工作并且无法访问用户存储。 So I use a byte array to store my image data which was cropped and resized by using the library Thumbnailator: 因此,我使用字节数组来存储通过使用Thumbnailator库裁剪并调整大小的图像数据:

public byte[] resize(String filepath) throws IOException{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        this.setBackground(Color.RED);
//      System.out.println("public File resize(String filepath)...");

        Thumbnails.of((new URL(filepath)))
        .size(256, 256)
        .outputQuality(1.0f)
        .outputFormat("jpg")
        .toOutputStream(outStream);

        byte[] bosArray = null; 
        bosArray = outStream.toByteArray();

        return bosArray;
    }

that is the call of resize() in my init(): 那是我的init()中resize()的调用:

try {
            if (ONLINE){
                String fname = FILENAME;
                if(getCodeBase()!=null)
                    fname = getCodeBase() + FILENAME;

                input = resize(fname);
            } 
            else {
                inputF = resize(new File(FILENAME));
            }

        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

and FILENAME is defined as a field: FILENAME被定义为一个字段:

private static final String FILENAME = "mountains.png";

in another class ImagePanel I transfer the byte array into a BufferedImage: 在另一个类ImagePanel中,我将字节数组传输到BufferedImage中:

public class ImagePanel extends JScrollPane {

private BufferedImage img;
private int width;
private int height;
private int[] histo;
private int[] normHisto; // normalized histogram
private int histogramHeight = 256;
private double variance;
private double expectation;

private int maxValueInHisto = 0;

/**
 * draw the image by using the stored BufferedImage
 */
public void paint(Graphics g) {
    g.drawImage(img, 0, 0, null);
}

..... .....

public ImagePanel(byte[] input) {

        try {
            //convert byte array back to BufferedImage
            InputStream in = new ByteArrayInputStream(input);
            img = ImageIO.read(in);

        } catch (IOException e) {
        }

        updateValues();
        this.setSize(width, height);
    } 

And everything works in the applet viewer of Eclipse but in the browser.. nothing happens. 一切都可以在Eclipse的applet浏览器中运行,但是在浏览器中都可以。 It loads the applet and show nothing even no error message. 它加载小程序,甚至不显示任何错误消息。

If I comment everything out and leave only the init of the applet and setting a background color -> it works. 如果我注释掉所有内容,只保留applet的init并设置背景色->则可以。 So that means that my html code should be fine 所以这意味着我的html代码应该没问题

<applet width="1000" height="1000" code="filterpackage.mainView.class" archive="Thumbnailator-0.3.10-all.jar"/>  

I tested it offline with Xampp and online in Chrome, Safari, Firefox but nothing happens, no error but no panel, no GUI.. nothing. 我使用Xampp进行了离线测试,并在Chrome,Safari,Firefox中进行了在线测试,但没有任何反应,没有错误,但没有面板,没有GUI ..没有。

Do you know what is wrong? 你知道出什么事了吗 Or do you have a tip what I could try? 或您有小费,我可以尝试吗?

It would be great if someone can help me I spent so much time until now and I don't find the solution :( 如果有人可以帮助我,那我会花很多时间直到现在却找不到解决方案:(

if you run your applet from a local HDD the code like 如果您从本地HDD运行小程序,则代码如下

getCodeBase() + FILENAME

definitely returns a local applet file path like a (in windows case) 肯定会返回本地小程序文件路径,例如(在Windows情况下)

file: C:/.../... 文件:C:/ ... / ...

So file: protocol is not the http: protocol so that may cause the problem with your net image to download according to the said path... Moreover, you say you have unsigned applet... 所以file:协议不是http:协议,因此可能导致网络映像根据所述路径下载出现问题。此外,您说您的未签名applet ...

As a simple way, you should place the applet to the Tomcat webapp folder and run it in this manner 作为一种简单的方法,您应该将小程序放置到Tomcat webapp文件夹中并以这种方式运行

http://localhost:8080/myappletpackage/applet.html http:// localhost:8080 / myappletpackage / applet.html

etc 等等

If you have some additional question details do comment 如果您还有其他疑问,请发表评论

Report that helps 报告有助于

Good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM