-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoPlayer.java
229 lines (185 loc) · 8.06 KB
/
VideoPlayer.java
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class VideoPlayer {
private static String filename = "";
private static int analysis = 0;
private static int antiAliasing = 0;
private static float scaleHeight = 1.0f;
private static float scaleWidth = 1.0f;
private static float fps = 10.0f;
static int IMAGE_TYPE = BufferedImage.TYPE_INT_RGB;
private static int originalWidth = 960;
private static int originalHeighht = 540;
private static void preProcessing(String[] args) {
fps = Float.parseFloat(args[3]);
antiAliasing = args.length > 4 ? Integer.parseInt(args[4]) : antiAliasing;
analysis = args.length > 5 ? Integer.parseInt(args[5]) : analysis;
scaleWidth = Float.parseFloat(args[1]);
scaleHeight = Float.parseFloat(args[2]);
filename = args[0];
System.out.println(" ********* Input Stats ************");
System.out.println(" ---------------------------------------------------------");
System.out.println(" | Video File Name | " + filename);
System.out.println(" ---------------------------------------------------------");
System.out.println(" | Height Scaling Factor | " + scaleWidth);
System.out.println(" ---------------------------------------------------------");
System.out.println(" | Width Scaling Factor | " + scaleWidth);
System.out.println(" ---------------------------------------------------------");
System.out.println(" | Frame rate | " + fps);
System.out.println(" ---------------------------------------------------------");
System.out.println(" | Anti-Aliasing | " + (antiAliasing > 0 ? "Yes" : "No"));
System.out.println(" ---------------------------------------------------------");
}
public static void main(String[] args) {
preProcessing(args);
GraphicalUserInterface graphicalUserInterface = new GraphicalUserInterface();
int modifiedWidth = originalWidth;
int modifiedHeight = originalHeighht;
boolean isScaled = false;
//Update the new width
if (scaleWidth < 1.0) {
modifiedWidth = (int) Math.floor(scaleWidth * originalWidth);
isScaled = true;
}
//Update the new getHeight
if (scaleHeight < 1.0) {
modifiedHeight = (int) Math.floor(scaleHeight * originalHeighht);
isScaled = true;
}
VideoFrameController videoCapture = new VideoFrameController(filename, originalWidth, originalHeighht);
ImageInfo originalFrame = new ImageInfo(originalWidth, originalHeighht, IMAGE_TYPE);
ImageInfo modifiedFrame = new ImageInfo(modifiedWidth, modifiedHeight, IMAGE_TYPE);
videoCapture.openFile();
int totalFrames = videoCapture.getNumOfFrames();
if (!videoCapture.isFileOpened()) {
System.err.println("Unable to open the vidoe stream .... for file : " + filename);
return;
}
if (scaleHeight > 1 && scaleWidth > 1 && analysis == 2) {
System.out.println("Seam carving uses to reduce the image size based on energy of the pixels. scaling up is not possible");
return;
}
List<BufferedImage> video = new ArrayList<BufferedImage>();
int frameCounter = 1;
System.out.println("Processing " + filename + "...Please Wait");
while (videoCapture.readContent(originalFrame)) {
System.out.print(".");
if (isScaled && analysis == 2) {
int reduceHeight = originalHeighht - modifiedHeight;
int reduceWidth = originalWidth - modifiedWidth;
SeamCarverEngine imageSeamOperations = new SeamCarverEngine(originalFrame.getImg());
int stepsFrwd = Math.max(modifiedHeight, modifiedWidth);
for (int kdx = 0; kdx < stepsFrwd; kdx++) {
if (reduceHeight > 0) {
imageSeamOperations.carveHorizontalSeam();
reduceHeight = reduceHeight - 1;
}
if (reduceWidth > 0) {
imageSeamOperations.carveVerticalSeam();
reduceWidth = reduceWidth - 1;
}
}
video.add(new BufferedImage(imageSeamOperations.getImg().getColorModel(), imageSeamOperations.getImg().copyData(null), false, null));
} else if (isScaled && analysis != 2) {
ImageInfo.resize(originalFrame, modifiedFrame, scaleWidth, scaleHeight, antiAliasing, analysis);
video.add(modifiedFrame.copyData());
} else {
video.add(originalFrame.copyData());
}
frameCounter++;
}
System.out.println("\nTotal Frames ......" + video.size());
System.out.println("Started showing Frames ......");
long initialTime = System.currentTimeMillis();
long sleep = (long) Math.floor((double) 1000 / fps);
for (int idx = 0; idx < video.size(); idx++) {
graphicalUserInterface.showFrame(video.get(idx), idx);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long endTime = System.currentTimeMillis();
System.out.println("Done.......Total Time took to display " + video.size() + " frames is(sec) " + (endTime - (initialTime + (video.size() * sleep) / 1000)) / 1000);
}
}
class GraphicalUserInterface {
private JFrame window;
private JLabel label;
public GraphicalUserInterface() {
this.label = new JLabel();
this.window = new JFrame();
window.getContentPane().add(label, BorderLayout.CENTER);
window.pack();
window.setVisible(true);
}
public void showFrame(BufferedImage bufferedImage, int frameCount) {
this.label.setIcon(new ImageIcon(bufferedImage));
window.pack();
}
}
class VideoFrameController {
private byte[] vidFrameData;
private String filename;
private boolean isFileOpened;
private int width;
private int colorModel;
private int height;
private int length;
private InputStream inputStream;
private int perFrameDataLength;
private int numOfFrames;
public boolean retrieveFrame(ImageInfo frame) {
frame.setBufferedImage(this.vidFrameData);
return true;
}
public boolean readContent(ImageInfo imageInfo) {
return grabFrame() ? retrieveFrame(imageInfo) : false;
}
public void openFile() {
File file = new File(this.filename);
try {
this.inputStream = new FileInputStream(file);
} catch (Exception e) {
e.printStackTrace();
}
this.length = (int) file.length();
this.perFrameDataLength = this.height * this.width * 3;
this.numOfFrames = this.length / perFrameDataLength;
isFileOpened = true;
}
public VideoFrameController(String filename, int width, int height) {
this.width = width;
this.height = height;
this.colorModel = BufferedImage.TYPE_INT_RGB;
this.filename = filename;
}
public boolean grabFrame() {
try {
int offset = 0, numRead = 0;
this.vidFrameData = new byte[this.perFrameDataLength];
while (offset < this.vidFrameData.length &&
(numRead = this.inputStream.read(this.vidFrameData, offset, this.vidFrameData.length - offset)) >= 0) {
offset = offset + numRead;
}
return numRead > 0 ? true : false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
public boolean isFileOpened() {
return this.isFileOpened;
}
public int getNumOfFrames() {
return numOfFrames;
}
}