Skip to content

Commit 774962a

Browse files
committed
Adds video support
ImageIcon(xxx,"jsvideo") where xxx is byte[] video data String video filename URL video URL
1 parent 63f31b8 commit 774962a

File tree

20 files changed

+379
-106
lines changed

20 files changed

+379
-106
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200417142842
1+
20200419115937
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20200417142842
1+
20200419115937

sources/net.sf.j2s.java.core/src/java/awt/image/BufferedImage.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import swingjs.JSGraphics2D;
5151
import swingjs.JSGraphicsCompositor;
5252
import swingjs.JSUtil;
53+
import swingjs.api.JSUtilI;
5354
import swingjs.api.js.DOMNode;
5455
import swingjs.api.js.HTML5Canvas;
5556

@@ -250,7 +251,7 @@ public class BufferedImage extends Image implements RenderedImage, Transparency
250251
* in a single byte array in the order B, G, R, A from lower to higher byte
251252
* addresses within each pixel.
252253
*/
253-
public static final int TYPE_4BYTE_HTML5 = -6;
254+
public static final int TYPE_4BYTE_HTML5 = JSUtilI.TYPE_4BYTE_HTML5;
254255
/**
255256
* Represents an image with 8-bit RGBA color components with the colors Blue,
256257
* Green, and Red stored in 3 bytes and 1 byte of alpha. The image has a
@@ -389,6 +390,9 @@ public class BufferedImage extends Image implements RenderedImage, Transparency
389390
* @see #TYPE_USHORT_555_RGB
390391
*/
391392
public BufferedImage(int width, int height, int imageType) {
393+
秘init(width, height, imageType);
394+
}
395+
protected void 秘init(int width, int height, int imageType) {
392396
this.width = width;
393397
this.height = height;
394398
秘wxh = width * height;

sources/net.sf.j2s.java.core/src/java/io/File.java

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,7 @@ private static File generateFile(String prefix, String suffix, File dir)
17531753
f.秘isTempFile = true;
17541754
return f;
17551755
}
1756+
17561757
//
17571758
// private static boolean checkAndCreate(String filename, SecurityManager sm,
17581759
// boolean restrictive)
@@ -1771,29 +1772,19 @@ private static File generateFile(String prefix, String suffix, File dir)
17711772
// return fs.createFileExclusively(filename, restrictive);
17721773
// }
17731774
//
1774-
// The resulting temporary file may have more restrictive access permission
1775-
// on some platforms, if restrictive is true.
1776-
private static File createTempFile0(String prefix, String suffix,
1777-
File directory, boolean restrictive)
1778-
throws IOException
1779-
{
1780-
if (prefix == null) throw new NullPointerException();
1781-
if (prefix.length() < 3)
1782-
throw new IllegalArgumentException("Prefix string too short");
1783-
String s = (suffix == null) ? ".tmp" : suffix;
1784-
directory = new File(temporaryDirectory + (directory == null ? "" : directory));
1785-
// we ensure that there is a clear marker for a temporary directory in SwingJS
1786-
// if (directory == null) {
1787-
// String tmpDir = temporaryDirectory();
1788-
// directory = new File(tmpDir);//, fs.prefixLength(tmpDir));
1789-
// }
1790-
// SecurityManager sm = System.getSecurityManager();
1791-
File f;
1792-
// do {
1793-
f = generateFile(prefix, s, directory);
1794-
// } while (!checkAndCreate(f.getPath(), sm, restrictive));
1795-
return f;
1796-
}
1775+
// The resulting temporary file may have more restrictive access permission
1776+
// on some platforms, if restrictive is true.
1777+
private static File createTempFile0(String prefix, String suffix, File directory, boolean restrictive)
1778+
throws IOException {
1779+
if (prefix == null)
1780+
throw new NullPointerException();
1781+
if (prefix.length() < 3)
1782+
throw new IllegalArgumentException("Prefix string too short");
1783+
String s = (suffix == null) ? ".tmp" : suffix;
1784+
directory = new File(temporaryDirectory + (directory == null ? "" : directory));
1785+
// we ensure that there is a clear marker for a temporary directory in SwingJS
1786+
return generateFile(prefix, s, directory);
1787+
}
17971788

17981789
/**
17991790
* <p> Creates a new empty file in the specified directory, using the

sources/net.sf.j2s.java.core/src/java/net/URL.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,9 +1007,11 @@ public String toExternalForm() {
10071007
* @return a URI instance equivalent to this URL.
10081008
* @since 1.5
10091009
*/
1010-
// public URI toURI() throws URISyntaxException {
1011-
// return new URI (toString());
1012-
// }
1010+
public URI toURI() throws URISyntaxException {
1011+
URI uri = new URI (toString());
1012+
uri.秘bytes = (byte[]) _streamData;
1013+
return uri;
1014+
}
10131015

10141016
/**
10151017
* Returns a <code>URLConnection</code> object that represents a connection to

sources/net.sf.j2s.java.core/src/javax/swing/ImageIcon.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.awt.image.BufferedImage;
3737
import java.awt.image.ImageObserver;
3838
import swingjs.JSGraphics2D;
39+
import swingjs.JSToolkit;
3940

4041
/**
4142
* An implementation of the Icon interface that paints Icons
@@ -148,7 +149,11 @@ public class ImageIcon implements Icon {
148149
* @see #ImageIcon(String)
149150
*/
150151
public ImageIcon(String filename, String description) {
151-
image = Toolkit.getDefaultToolkit().getImage(filename);
152+
if (description != null && description.indexOf("jsvideo") >= 0) {
153+
image = ((JSToolkit)Toolkit.getDefaultToolkit()).getVideo(filename);
154+
} else {
155+
image = Toolkit.getDefaultToolkit().getImage(filename);
156+
}
152157
if (image == null) {
153158
return;
154159
}
@@ -186,7 +191,11 @@ public ImageIcon (String filename) {
186191
* @see #ImageIcon(String)
187192
*/
188193
public ImageIcon(URL location, String description) {
189-
image = Toolkit.getDefaultToolkit().getImage(location);
194+
if (description != null && description.indexOf("jsvideo") >= 0) {
195+
image = ((JSToolkit)Toolkit.getDefaultToolkit()).getVideo(location);
196+
} else {
197+
image = Toolkit.getDefaultToolkit().getImage(location);
198+
}
190199
if (image == null) {
191200
return;
192201
}
@@ -249,7 +258,11 @@ public ImageIcon (Image image) {
249258
* @see java.awt.Toolkit#createImage
250259
*/
251260
public ImageIcon (byte[] imageData, String description) {
252-
this.image = Toolkit.getDefaultToolkit().createImage(imageData);
261+
if (description != null && description.indexOf("jsvideo") >= 0) {
262+
image = ((JSToolkit)Toolkit.getDefaultToolkit()).createVideo(imageData);
263+
} else {
264+
this.image = Toolkit.getDefaultToolkit().createImage(imageData);
265+
}
253266
if (image == null) {
254267
return;
255268
}
@@ -315,6 +328,16 @@ protected void loadImage(Image image) {
315328
// }
316329
}
317330

331+
/**
332+
* SwingJS delayed video size
333+
*
334+
* @param w
335+
* @param h
336+
*/
337+
public void 秘setIconSize(int w, int h) {
338+
width = w;
339+
height = h;
340+
}
318341
// /**
319342
// * Returns an ID to use with the MediaTracker in loading an image.
320343
// */

sources/net.sf.j2s.java.core/src/swingjs/JSImage.java

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33
import java.awt.Component;
44
import java.awt.Image;
55
import java.awt.image.BufferedImage;
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.nio.file.Path;
9+
10+
import javax.swing.ImageIcon;
11+
import javax.swing.JLabel;
12+
import javax.swing.SwingUtilities;
613

714
import javajs.util.Base64;
15+
import swingjs.JSFileSystem.JSPath;
816
import swingjs.api.js.DOMNode;
17+
import swingjs.plaf.JSComponentUI;
918

1019
/**
1120
* A JavaScript version of BufferedImage.
@@ -61,14 +70,63 @@ public JSImage(byte[] pixelBytes, int width, int height, String src) {
6170
* @param type
6271
*/
6372
@SuppressWarnings("unused")
64-
public void getDOMImage(byte[] b, String type) {
65-
String dataurl = "data:image/" + type + ";base64," + Base64.getBase64(b).toString();
73+
void setImageNode(JSPath source, byte[] b, String type) {
6674
DOMNode img = null;
67-
/**
68-
* @j2sNative img = new Image(this.width, this.height); //if (this.callback)
69-
* img.onload = this.callback; img.src = dataurl;
70-
*/
75+
if (type == "video") {
76+
try {
77+
String src = (source == null ? null : JSUtil.getWebPathFor(source.toString()));
78+
if (b == null && source != null)
79+
b = source.秘bytes;
80+
System.out.println("JSImage video " + src + " " + (b == null ? 0 : b.length));
81+
img = DOMNode.createElement("video", File.createTempFile("video_", "").getName());
82+
DOMNode node = img;
83+
Runnable r = new Runnable() {
84+
// set dimension when available
85+
@Override
86+
public void run() {
87+
int w = 0, h = 0;
88+
DOMNode n = node;
89+
/**
90+
* @j2sNative
91+
*
92+
* w = n.width = n.videoWidth; h = n.height = n.videoHeight;
93+
*
94+
*/
95+
JSComponentUI ui = (JSComponentUI) DOMNode.getAttr(node, "data-ui");
96+
System.out.println("JSImage w,h " + w + " " + h);
97+
秘init(w, h, TYPE_INT_ARGB);
98+
if (ui != null && ui.jc instanceof JLabel) {
99+
JLabel label = (JLabel) ui.jc;
100+
w = label.getWidth();
101+
h = label.getHeight();
102+
n.setAttribute("width", w + "");
103+
n.setAttribute("height", h + "");
104+
ui.setTainted();
105+
ImageIcon icon = (ImageIcon) label.getIcon();
106+
if (icon != null) {
107+
icon.秘setIconSize(w, h);
108+
}
109+
}
110+
}
111+
};
112+
/**
113+
* @j2sNative
114+
*
115+
* //document.body.appendChild(img);
116+
* img.src = (b == null ? src : URL.createObjectURL(new Blob([b])));
117+
* img.onloadedmetadata = function(){ r.run$()};
118+
* img.load();
119+
*/
120+
121+
} catch (IOException e) {
122+
}
123+
} else {
124+
String dataurl = "data:image/" + type + ";base64," + Base64.getBase64(b).toString();
125+
/**
126+
* @j2sNative img = new Image(this.width, this.height); img.src = dataurl;
127+
*/
71128

129+
}
72130
秘imgNode = img;
73131
}
74132

sources/net.sf.j2s.java.core/src/swingjs/JSImagekit.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import java.awt.image.ColorModel;
1616
import java.awt.image.ImageConsumer;
1717
import java.awt.image.WritableRaster;
18+
import java.nio.file.Path;
1819

20+
import swingjs.JSFileSystem.JSPath;
1921
import swingjs.api.Interface;
2022
import swingjs.api.js.DOMNode;
2123
import swingjs.api.js.HTML5Canvas;
@@ -36,6 +38,7 @@ public class JSImagekit implements ImageConsumer {
3638
private static final int JPG = 1;
3739
private static final int GIF = 2;
3840
private static final int BMP = 3;
41+
private static final int VIDEO = 4;
3942
private static final int JPG_SOF0 = 0xC0FF;
4043
private static final int JPG_SOF2 = 0xC2FF;
4144

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

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

156-
private static JSImage createImageFromBytesStatic(byte[] data, int imageoffset,
157-
int imagelength, String name) {
159+
private static JSImage createImageFromBytesStatic(byte[] data, int imageoffset, int imagelength, String name,
160+
int imageType) {
158161
int w = 0, h = 0;
159162
int[] argb = null;
160163
byte[] b = null;
@@ -163,20 +166,24 @@ private static JSImage createImageFromBytesStatic(byte[] data, int imageoffset,
163166
// this is from Component.createImage();
164167
w = imageoffset;
165168
h = imagelength;
169+
} else if (imageType == VIDEO){
170+
b = data;
171+
w = imageoffset;
172+
h = imagelength;
173+
type = "video";
166174
} else {
167175
if (imagelength < 0)
168176
imagelength = data.length;
169-
// not implemented in JavaScript:
177+
// not implemented in JavaScript:
170178
// b = Arrays.copyOfRange(data, imageoffset, imagelength);
171179
int n = imagelength - imageoffset;
172-
System.arraycopy(data, imageoffset, b = new byte[n], 0, n);
173-
if (b.length < 10)//was 54??? I have no recollection of why that might be.
180+
System.arraycopy(data, imageoffset, b = new byte[n], 0, n);
181+
if (b.length < 10)// was 54??? I have no recollection of why that might be.
174182
return null;
175-
switch (getSourceType(b)) {
183+
switch (imageType == UNK ? getSourceType(b) : imageType) {
176184
case BMP:
177185
// just get bytes directly
178-
BMPDecoder ie = (BMPDecoder) Interface.getInstance(
179-
"javajs.img.BMPDecoder", true);
186+
BMPDecoder ie = (BMPDecoder) Interface.getInstance("javajs.img.BMPDecoder", true);
180187
Object[] o = ie.decodeWindowsBMP(b);
181188
if (o == null || o[0] == null)
182189
return null;
@@ -212,17 +219,16 @@ private static JSImage createImageFromBytesStatic(byte[] data, int imageoffset,
212219
type = "gif";
213220
break;
214221
case UNK:
215-
System.out.println("JSImagekit: Unknown image type: " + b[0] + " "
216-
+ b[1] + " " + b[2] + " " + b[3]);
222+
System.out.println("JSImagekit: Unknown image type: " + b[0] + " " + b[1] + " " + b[2] + " " + b[3]);
217223
data = null;
218224
break;
219225
}
220226
}
221227
if (w == 0 || h == 0)
222228
return null;
223-
JSImage jsimage = new JSImage(argb, w, h, name);
229+
JSImage jsimage = new JSImage(argb, w, h, name);
224230
if (data != null && argb == null)
225-
jsimage.getDOMImage(b, type);
231+
jsimage.setImageNode(null, b, type);
226232
return jsimage;
227233
}
228234

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

307+
public Image createVideo(Path path) {
308+
JSImage jsimage = new JSImage((byte[])null, 1, 1, path.toString());
309+
jsimage.setImageNode((JSPath) path, null, "video");
310+
return jsimage;
311+
}
312+
313+
public Image createVideo(byte[] bytes) {
314+
return createImageFromBytesStatic(bytes, 1, 1, null, VIDEO);
315+
}
316+
301317

302318
}

0 commit comments

Comments
 (0)