如何将图像从java

如何将图像从java

本文介绍了如何将图像从java applet发送到javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在JAVA中有代码

import java.applet.Applet; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.Image; import java.awt.MediaTracker; import java.awt.TextArea; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.DataFlavor; import java.awt.datatransfer.UnsupportedFlavorException; import java.awt.image.BufferedImage; import java.awt.image.ImageObserver; import java.awt.image.ImageProducer; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO;

public class JavaScriptToJava extends Applet{

    TextArea textBox;
    Image img;
    MediaTracker tr;
    public void init(){
        setLayout(new FlowLayout());
        textBox = new TextArea(5,40);
        add(textBox);
    }

    public void appendText(String text){
        textBox.append(text);

           Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
                try {
                        //Get data from clipboard and assign it to an image.
                        //clipboard.getData() returns an object, so we need to cast it to a BufferdImage.
                        BufferedImage image = (BufferedImage)clipboard.getData(DataFlavor.imageFlavor);


                }
                //getData throws this.
                catch(UnsupportedFlavorException ufe) {
                        ufe.printStackTrace();
                }

                catch(IOException ioe) {
                        ioe.printStackTrace();
                }
    }



}

我需要在javascript中获取此图片。怎么做?

I need get this image in javascript. How do this?

我的主要任务是从剪贴板复制图像并将其传输到调用js的html页面

My main task is to copy the images from the clipboard and transfer it to the html page calling js

推荐答案

创建一个Java方法以从BufferedImage获取像素:

Create a Java method to get a pixel from your BufferedImage:

public int getPixel(int xLoc, int yLoc) {
    int argb = myImage.getRGB(xLoc, yLoc);
    int rgba = // ... convert to rgba;
    return rgba;
}

我认为也应该可以只调用一次applet并通过数组到您的JavaScript代码。

I think it should also be possible to call the applet just once and pass an array to your JavaScript code.

在JavaScript中,创建一个Canvas元素,然后为每个像素调用applet并在Canvas中设置。 ()( )

In JavaScript, create a Canvas element, then call applet for each pixel and set in Canvas. (More info about how to call applet from JavaScript.) (Tutorial on HMTL Canvas pixel manipulation.)

更进一步:

var myCanvas = document.getElementById("myCanvas");
var myImg    = myCanvas.toDataURL("image/png");
document.write('<img src="'+myImg+'"/>');

这篇关于如何将图像从java applet发送到javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 15:03