Skip to content

Commit

Permalink
Adds video support
Browse files Browse the repository at this point in the history
ImageIcon(xxx,"jsvideo")

where xxx is

byte[] video data
String video filename
URL    video URL
  • Loading branch information
BobHanson committed Apr 19, 2020
1 parent 63f31b8 commit 774962a
Show file tree
Hide file tree
Showing 20 changed files with 379 additions and 106 deletions.
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/swingjs/timestamp
@@ -1 +1 @@
20200417142842
20200419115937
2 changes: 1 addition & 1 deletion sources/net.sf.j2s.core/dist/swingjs/ver/3.2.9/timestamp
@@ -1 +1 @@
20200417142842
20200419115937
Expand Up @@ -50,6 +50,7 @@
import swingjs.JSGraphics2D;
import swingjs.JSGraphicsCompositor;
import swingjs.JSUtil;
import swingjs.api.JSUtilI;
import swingjs.api.js.DOMNode;
import swingjs.api.js.HTML5Canvas;

Expand Down Expand Up @@ -250,7 +251,7 @@ public class BufferedImage extends Image implements RenderedImage, Transparency
* in a single byte array in the order B, G, R, A from lower to higher byte
* addresses within each pixel.
*/
public static final int TYPE_4BYTE_HTML5 = -6;
public static final int TYPE_4BYTE_HTML5 = JSUtilI.TYPE_4BYTE_HTML5;
/**
* Represents an image with 8-bit RGBA color components with the colors Blue,
* Green, and Red stored in 3 bytes and 1 byte of alpha. The image has a
Expand Down Expand Up @@ -389,6 +390,9 @@ public class BufferedImage extends Image implements RenderedImage, Transparency
* @see #TYPE_USHORT_555_RGB
*/
public BufferedImage(int width, int height, int imageType) {
init(width, height, imageType);
}
protected voidinit(int width, int height, int imageType) {
this.width = width;
this.height = height;
wxh = width * height;
Expand Down
37 changes: 14 additions & 23 deletions sources/net.sf.j2s.java.core/src/java/io/File.java
Expand Up @@ -1753,6 +1753,7 @@ private static File generateFile(String prefix, String suffix, File dir)
f.秘isTempFile = true;
return f;
}

//
// private static boolean checkAndCreate(String filename, SecurityManager sm,
// boolean restrictive)
Expand All @@ -1771,29 +1772,19 @@ private static File generateFile(String prefix, String suffix, File dir)
// return fs.createFileExclusively(filename, restrictive);
// }
//
// The resulting temporary file may have more restrictive access permission
// on some platforms, if restrictive is true.
private static File createTempFile0(String prefix, String suffix,
File directory, boolean restrictive)
throws IOException
{
if (prefix == null) throw new NullPointerException();
if (prefix.length() < 3)
throw new IllegalArgumentException("Prefix string too short");
String s = (suffix == null) ? ".tmp" : suffix;
directory = new File(temporaryDirectory + (directory == null ? "" : directory));
// we ensure that there is a clear marker for a temporary directory in SwingJS
// if (directory == null) {
// String tmpDir = temporaryDirectory();
// directory = new File(tmpDir);//, fs.prefixLength(tmpDir));
// }
// SecurityManager sm = System.getSecurityManager();
File f;
// do {
f = generateFile(prefix, s, directory);
// } while (!checkAndCreate(f.getPath(), sm, restrictive));
return f;
}
// The resulting temporary file may have more restrictive access permission
// on some platforms, if restrictive is true.
private static File createTempFile0(String prefix, String suffix, File directory, boolean restrictive)
throws IOException {
if (prefix == null)
throw new NullPointerException();
if (prefix.length() < 3)
throw new IllegalArgumentException("Prefix string too short");
String s = (suffix == null) ? ".tmp" : suffix;
directory = new File(temporaryDirectory + (directory == null ? "" : directory));
// we ensure that there is a clear marker for a temporary directory in SwingJS
return generateFile(prefix, s, directory);
}

/**
* <p> Creates a new empty file in the specified directory, using the
Expand Down
8 changes: 5 additions & 3 deletions sources/net.sf.j2s.java.core/src/java/net/URL.java
Expand Up @@ -1007,9 +1007,11 @@ public String toExternalForm() {
* @return a URI instance equivalent to this URL.
* @since 1.5
*/
// public URI toURI() throws URISyntaxException {
// return new URI (toString());
// }
public URI toURI() throws URISyntaxException {
URI uri = new URI (toString());
uri.秘bytes = (byte[]) _streamData;
return uri;
}

/**
* Returns a <code>URLConnection</code> object that represents a connection to
Expand Down
29 changes: 26 additions & 3 deletions sources/net.sf.j2s.java.core/src/javax/swing/ImageIcon.java
Expand Up @@ -36,6 +36,7 @@
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import swingjs.JSGraphics2D;
import swingjs.JSToolkit;

/**
* An implementation of the Icon interface that paints Icons
Expand Down Expand Up @@ -148,7 +149,11 @@ public class ImageIcon implements Icon {
* @see #ImageIcon(String)
*/
public ImageIcon(String filename, String description) {
image = Toolkit.getDefaultToolkit().getImage(filename);
if (description != null && description.indexOf("jsvideo") >= 0) {
image = ((JSToolkit)Toolkit.getDefaultToolkit()).getVideo(filename);
} else {
image = Toolkit.getDefaultToolkit().getImage(filename);
}
if (image == null) {
return;
}
Expand Down Expand Up @@ -186,7 +191,11 @@ public ImageIcon (String filename) {
* @see #ImageIcon(String)
*/
public ImageIcon(URL location, String description) {
image = Toolkit.getDefaultToolkit().getImage(location);
if (description != null && description.indexOf("jsvideo") >= 0) {
image = ((JSToolkit)Toolkit.getDefaultToolkit()).getVideo(location);
} else {
image = Toolkit.getDefaultToolkit().getImage(location);
}
if (image == null) {
return;
}
Expand Down Expand Up @@ -249,7 +258,11 @@ public ImageIcon (Image image) {
* @see java.awt.Toolkit#createImage
*/
public ImageIcon (byte[] imageData, String description) {
this.image = Toolkit.getDefaultToolkit().createImage(imageData);
if (description != null && description.indexOf("jsvideo") >= 0) {
image = ((JSToolkit)Toolkit.getDefaultToolkit()).createVideo(imageData);
} else {
this.image = Toolkit.getDefaultToolkit().createImage(imageData);
}
if (image == null) {
return;
}
Expand Down Expand Up @@ -315,6 +328,16 @@ protected void loadImage(Image image) {
// }
}

/**
* SwingJS delayed video size
*
* @param w
* @param h
*/
public voidsetIconSize(int w, int h) {
width = w;
height = h;
}
// /**
// * Returns an ID to use with the MediaTracker in loading an image.
// */
Expand Down
70 changes: 64 additions & 6 deletions sources/net.sf.j2s.java.core/src/swingjs/JSImage.java
Expand Up @@ -3,9 +3,18 @@
import java.awt.Component;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

import javajs.util.Base64;
import swingjs.JSFileSystem.JSPath;
import swingjs.api.js.DOMNode;
import swingjs.plaf.JSComponentUI;

/**
* A JavaScript version of BufferedImage.
Expand Down Expand Up @@ -61,14 +70,63 @@ public JSImage(byte[] pixelBytes, int width, int height, String src) {
* @param type
*/
@SuppressWarnings("unused")
public void getDOMImage(byte[] b, String type) {
String dataurl = "data:image/" + type + ";base64," + Base64.getBase64(b).toString();
void setImageNode(JSPath source, byte[] b, String type) {
DOMNode img = null;
/**
* @j2sNative img = new Image(this.width, this.height); //if (this.callback)
* img.onload = this.callback; img.src = dataurl;
*/
if (type == "video") {
try {
String src = (source == null ? null : JSUtil.getWebPathFor(source.toString()));
if (b == null && source != null)
b = source.秘bytes;
System.out.println("JSImage video " + src + " " + (b == null ? 0 : b.length));
img = DOMNode.createElement("video", File.createTempFile("video_", "").getName());
DOMNode node = img;
Runnable r = new Runnable() {
// set dimension when available
@Override
public void run() {
int w = 0, h = 0;
DOMNode n = node;
/**
* @j2sNative
*
* w = n.width = n.videoWidth; h = n.height = n.videoHeight;
*
*/
JSComponentUI ui = (JSComponentUI) DOMNode.getAttr(node, "data-ui");
System.out.println("JSImage w,h " + w + " " + h);
init(w, h, TYPE_INT_ARGB);
if (ui != null && ui.jc instanceof JLabel) {
JLabel label = (JLabel) ui.jc;
w = label.getWidth();
h = label.getHeight();
n.setAttribute("width", w + "");
n.setAttribute("height", h + "");
ui.setTainted();
ImageIcon icon = (ImageIcon) label.getIcon();
if (icon != null) {
icon.秘setIconSize(w, h);
}
}
}
};
/**
* @j2sNative
*
* //document.body.appendChild(img);
* img.src = (b == null ? src : URL.createObjectURL(new Blob([b])));
* img.onloadedmetadata = function(){ r.run$()};
* img.load();
*/

} catch (IOException e) {
}
} else {
String dataurl = "data:image/" + type + ";base64," + Base64.getBase64(b).toString();
/**
* @j2sNative img = new Image(this.width, this.height); img.src = dataurl;
*/

}
imgNode = img;
}

Expand Down
42 changes: 29 additions & 13 deletions sources/net.sf.j2s.java.core/src/swingjs/JSImagekit.java
Expand Up @@ -15,7 +15,9 @@
import java.awt.image.ColorModel;
import java.awt.image.ImageConsumer;
import java.awt.image.WritableRaster;
import java.nio.file.Path;

import swingjs.JSFileSystem.JSPath;
import swingjs.api.Interface;
import swingjs.api.js.DOMNode;
import swingjs.api.js.HTML5Canvas;
Expand All @@ -36,6 +38,7 @@ public class JSImagekit implements ImageConsumer {
private static final int JPG = 1;
private static final int GIF = 2;
private static final int BMP = 3;
private static final int VIDEO = 4;
private static final int JPG_SOF0 = 0xC0FF;
private static final int JPG_SOF2 = 0xC2FF;

Expand All @@ -52,7 +55,7 @@ public class JSImagekit implements ImageConsumer {
*/
public JSImage createImageFromBytes(byte[] data, int imageoffset,
int imagelength, String name) {
return createImageFromBytesStatic(data, imageoffset, imagelength, name);
return createImageFromBytesStatic(data, imageoffset, imagelength, name, UNK);
}

private int width;
Expand Down Expand Up @@ -153,8 +156,8 @@ public void setPixels(int x, int y, int w, int h, ColorModel model,
JSUtil.notImplemented("byte-based image pixels");
}

private static JSImage createImageFromBytesStatic(byte[] data, int imageoffset,
int imagelength, String name) {
private static JSImage createImageFromBytesStatic(byte[] data, int imageoffset, int imagelength, String name,
int imageType) {
int w = 0, h = 0;
int[] argb = null;
byte[] b = null;
Expand All @@ -163,20 +166,24 @@ private static JSImage createImageFromBytesStatic(byte[] data, int imageoffset,
// this is from Component.createImage();
w = imageoffset;
h = imagelength;
} else if (imageType == VIDEO){
b = data;
w = imageoffset;
h = imagelength;
type = "video";
} else {
if (imagelength < 0)
imagelength = data.length;
// not implemented in JavaScript:
// not implemented in JavaScript:
// b = Arrays.copyOfRange(data, imageoffset, imagelength);
int n = imagelength - imageoffset;
System.arraycopy(data, imageoffset, b = new byte[n], 0, n);
if (b.length < 10)//was 54??? I have no recollection of why that might be.
System.arraycopy(data, imageoffset, b = new byte[n], 0, n);
if (b.length < 10)// was 54??? I have no recollection of why that might be.
return null;
switch (getSourceType(b)) {
switch (imageType == UNK ? getSourceType(b) : imageType) {
case BMP:
// just get bytes directly
BMPDecoder ie = (BMPDecoder) Interface.getInstance(
"javajs.img.BMPDecoder", true);
BMPDecoder ie = (BMPDecoder) Interface.getInstance("javajs.img.BMPDecoder", true);
Object[] o = ie.decodeWindowsBMP(b);
if (o == null || o[0] == null)
return null;
Expand Down Expand Up @@ -212,17 +219,16 @@ private static JSImage createImageFromBytesStatic(byte[] data, int imageoffset,
type = "gif";
break;
case UNK:
System.out.println("JSImagekit: Unknown image type: " + b[0] + " "
+ b[1] + " " + b[2] + " " + b[3]);
System.out.println("JSImagekit: Unknown image type: " + b[0] + " " + b[1] + " " + b[2] + " " + b[3]);
data = null;
break;
}
}
if (w == 0 || h == 0)
return null;
JSImage jsimage = new JSImage(argb, w, h, name);
JSImage jsimage = new JSImage(argb, w, h, name);
if (data != null && argb == null)
jsimage.getDOMImage(b, type);
jsimage.setImageNode(null, b, type);
return jsimage;
}

Expand Down Expand Up @@ -298,5 +304,15 @@ public static JSGraphics2D createCanvasGraphics(int width, int height, String id
return new JSGraphics2D(canvas);
}

public Image createVideo(Path path) {
JSImage jsimage = new JSImage((byte[])null, 1, 1, path.toString());
jsimage.setImageNode((JSPath) path, null, "video");
return jsimage;
}

public Image createVideo(byte[] bytes) {
return createImageFromBytesStatic(bytes, 1, 1, null, VIDEO);
}


}

0 comments on commit 774962a

Please sign in to comment.