admin管理员组

文章数量:1418411

I am making a function that pares two pictures if they are the same. I have this code:

var rez = null;

rez = pareImages();

alert(rez);


boolean
function pareImages() {
  BufferedImage bi1 = java.ImageIO.read(new File("C:\\MyFiles\\pic1.png")),
    bi2 = java.ImageIO.read(new File("C:\\MyFiles\\pic2.png"));
  Raster r1 = bi1.getData(),
    r2 = bi2.getData();
  DataBuffer db1 = r1.getDataBuffer(),
    db2 = r2.getDataBuffer();
  int size1 = db1.getSize(),
    size2 = db2.getSize();

  // checking if the files sizes are the same
  if (size1 != size2)
    return false;

  // pixel by pixel check up
  for (int i = 0; i < size1; i++)
    if (db1.getElem(i) != db2.getElem(i))
      return false;

  return true;
}

Now I want to run this code in .js file but when I do I get an error, missing ";" error. So how do I make this function javascript patible ?

Thanks.

I am making a function that pares two pictures if they are the same. I have this code:

var rez = null;

rez = pareImages();

alert(rez);


boolean
function pareImages() {
  BufferedImage bi1 = java.ImageIO.read(new File("C:\\MyFiles\\pic1.png")),
    bi2 = java.ImageIO.read(new File("C:\\MyFiles\\pic2.png"));
  Raster r1 = bi1.getData(),
    r2 = bi2.getData();
  DataBuffer db1 = r1.getDataBuffer(),
    db2 = r2.getDataBuffer();
  int size1 = db1.getSize(),
    size2 = db2.getSize();

  // checking if the files sizes are the same
  if (size1 != size2)
    return false;

  // pixel by pixel check up
  for (int i = 0; i < size1; i++)
    if (db1.getElem(i) != db2.getElem(i))
      return false;

  return true;
}

Now I want to run this code in .js file but when I do I get an error, missing ";" error. So how do I make this function javascript patible ?

Thanks.

Share Improve this question edited Aug 9, 2022 at 10:51 Luca Kiebel 10.1k7 gold badges32 silver badges46 bronze badges asked Nov 17, 2012 at 0:16 edinvnodeedinvnode 3,5577 gold badges34 silver badges56 bronze badges 2
  • 1 en.wikipedia/wiki/JavaScript#JavaScript_and_Java – TheZ Commented Nov 17, 2012 at 0:27
  • Write the javascript bytecode interpreter, that will prob help you. – Roman C Commented Nov 17, 2012 at 0:37
Add a ment  | 

3 Answers 3

Reset to default 4

Unfortunately Javacript and Java are pletely seperate languages, and the only thing which they have in mon in the name, therefore this is impossible.

If you want to learn about doing something similar in javascript then take a look at the File API's: https://developer.mozilla/en-US/docs/DOM/File_APIs

You can't run Java code in JavaScript. You can either pile your Java code as an applet and embed that in you page, or you just rewrite your code in JS. If you use the html5 canvas and the getImageData() function, this should be doable. That way you can easily interact with your code from JavaScript.

It is somewhat possible to use Java classes and APIs in code that you write in JavaScript with Mozilla Rhino.

Rhino is simply a JS interpret written in Java that is bundled with the JDK. It basically allows you to use the Java APIs with JS syntax, but on the other hand, there is no DOM API available and you can't run such scripts in a Web browser. They are run by the JVM.

本文标签: run java in javascriptStack Overflow