-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJPEGCodec.java
More file actions
147 lines (137 loc) · 5.94 KB
/
JPEGCodec.java
File metadata and controls
147 lines (137 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* @(#)JPGCodec.java
*
* Copyright (c) 2011 Werner Randelshofer, Immensee, Switzerland.
* All rights reserved.
*
* You may not use, copy or modify this file, except in compliance with the
* license agreement you entered into with Werner Randelshofer.
* For details see accompanying license terms.
*/
import javax.imageio.ImageReader;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
/**
* {@code JPEGCodec} encodes a BufferedImage as a byte[] array.
* <p>
* Supported input formats:
* <ul>
* {@code VideoFormat} with {@code BufferedImage.class}, any width, any height,
* any depth.
* </ul>
* Supported output formats:
* <ul>
* {@code VideoFormat} with {@code byte[].class}, same width and height as input
* format, depth=24.
* </ul>
*
* @author Werner Randelshofer
* @version $Id: JPEGCodec.java 186 2012-03-28 11:18:42Z werner $
*/
public class JPEGCodec extends AbstractVideoCodec {
public JPEGCodec() {
super(new Format[]{
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_JAVA,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_BUFFERED_IMAGE), //
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_QUICKTIME,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_QUICKTIME_JPEG,//
VideoFormatKeys.CompressorNameKey, VideoFormatKeys.COMPRESSOR_NAME_QUICKTIME_JPEG, //
VideoFormatKeys.DataClassKey, byte[].class, VideoFormatKeys.DepthKey, 24), //
//
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_AVI,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_AVI_MJPG, VideoFormatKeys.DataClassKey, byte[].class, VideoFormatKeys.DepthKey, 24), //
},
new Format[]{
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_JAVA,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_BUFFERED_IMAGE), //
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_QUICKTIME,//
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_QUICKTIME_JPEG,//
VideoFormatKeys.CompressorNameKey, VideoFormatKeys.COMPRESSOR_NAME_QUICKTIME_JPEG, //
VideoFormatKeys.DataClassKey, byte[].class, VideoFormatKeys.DepthKey, 24), //
//
new Format(VideoFormatKeys.MediaTypeKey, FormatKeys.MediaType.VIDEO, VideoFormatKeys.MimeTypeKey, VideoFormatKeys.MIME_AVI,
VideoFormatKeys.EncodingKey, VideoFormatKeys.ENCODING_AVI_MJPG, VideoFormatKeys.DataClassKey, byte[].class, VideoFormatKeys.DepthKey, 24), //
}//
);
name = "JPEG Codec";
}
@Override
public int process(Buffer in, Buffer out) {
if (outputFormat.get(VideoFormatKeys.EncodingKey).equals(VideoFormatKeys.ENCODING_BUFFERED_IMAGE)) {
return decode(in, out);
} else {
return encode(in, out);
}
}
public int encode(Buffer in, Buffer out) {
out.setMetaTo(in);
out.format = outputFormat;
if (in.isFlag(BufferFlag.DISCARD)) {
return CODEC_OK;
}
BufferedImage image = getBufferedImage(in);
if (image == null) {
out.setFlag(BufferFlag.DISCARD);
return CODEC_FAILED;
}
ByteArrayImageOutputStream tmp;
if (out.data instanceof byte[]) {
tmp = new ByteArrayImageOutputStream((byte[]) out.data);
} else {
tmp = new ByteArrayImageOutputStream();
}
try {
ImageWriter iw = ImageIO.getImageWritersByMIMEType("image/jpeg").next();
ImageWriteParam iwParam = iw.getDefaultWriteParam();
iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
float quality = outputFormat.get(VideoFormatKeys.QualityKey, 1f);
iwParam.setCompressionQuality(quality);
iw.setOutput(tmp);
IIOImage img = new IIOImage(image, null, null);
iw.write(null, img, iwParam);
iw.dispose();
out.sampleCount = 1;
out.setFlag(BufferFlag.KEYFRAME);
out.data = tmp.getBuffer();
out.offset = 0;
out.length = (int) tmp.getStreamPosition();
return CODEC_OK;
} catch (IOException ex) {
ex.printStackTrace();
out.setFlag(BufferFlag.DISCARD);
return CODEC_FAILED;
}
}
public int decode(Buffer in, Buffer out) {
out.setMetaTo(in);
out.format = outputFormat;
if (in.isFlag(BufferFlag.DISCARD)) {
return CODEC_OK;
}
byte[] data = (byte[]) in.data;
if (data == null) {
out.setFlag(BufferFlag.DISCARD);
return CODEC_FAILED;
}
ByteArrayImageInputStream tmp = new ByteArrayImageInputStream(data);
try {
// ImageReader ir = (ImageReader) ImageIO.getImageReadersByMIMEType("image/jpeg").next();
ImageReader ir = new MJPGImageReader(new MJPGImageReaderSpi());
ir.setInput(tmp);
out.data = ir.read(0);
ir.dispose();
out.sampleCount = 1;
out.offset = 0;
out.length = (int) tmp.getStreamPosition();
return CODEC_OK;
} catch (IOException ex) {
ex.printStackTrace();
out.setFlag(BufferFlag.DISCARD);
return CODEC_FAILED;
}
}
}