Java 小程序无法在浏览器中工作 - 使用图像文件

发布于 2024-12-28 13:58:05 字数 2627 浏览 0 评论 0原文

我有一个 java 小程序,它演示了一些过滤器。我有一个示例图像的预定义路径,用户可以选择自己的图片。

我无法使用新文件,因为我正在浏览器中工作并且无法访问用户存储。因此,我使用字节数组来存储通过使用库 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;
    }

这是在我的 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();
        }

并且 FILENAME 被定义为一个字段:

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

在我传输的另一个类 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);
    } 

并且在 Eclipse 的 applet 查看器中一切正常,但在浏览器中..什么也没有发生。它加载小程序并且不显示任何内容,甚至没有错误消息。

如果我注释掉所有内容并只保留小程序的 init 并设置背景颜色 ->有用。所以这意味着我的 html 代码应该没问题,

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

我用 Xampp 离线测试,并在 Chrome、Safari、Firefox 中在线测试,但没有任何反应,没有错误,但没有面板,没有 GUI.. 什么也没有。

你知道出了什么问题吗?或者你有什么建议我可以尝试吗?

如果有人可以帮助我,那就太好了,我花了很多时间,直到现在还没有找到解决方案:(

I have a java applet which demonstrates some filters. 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:

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

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:

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

in another class ImagePanel I transfer the byte array into a 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. 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. So that means that my html code should be fine

<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.

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 :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍孤女 2025-01-04 13:58:05

如果您从本地 HDD 运行小程序,则代码

getCodeBase() + FILENAME

肯定会返回本地小程序文件路径,例如(在 Windows 情况下)

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

因此 file: 协议不是 http: 协议,因此可能会导致您的网络出现问题要根据上述路径下载的图像...此外,您说您有未签名的小程序...

作为一个简单的方法,您应该将小程序放置到Tomcat webapp文件夹中并以这种方式运行它

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

如果您有一些其他问题详细信息发表评论

有帮助的报告

祝你好运

if you run your applet from a local HDD the code like

getCodeBase() + FILENAME

definitely returns a local applet file path like a (in windows case)

file: 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...

As a simple way, you should place the applet to the Tomcat webapp folder and run it in this manner

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

etc

If you have some additional question details do comment

Report that helps

Good luck

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文