-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageReader&Printer(updating).java
222 lines (184 loc) · 6.09 KB
/
ImageReader&Printer(updating).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
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
//import javax.swing.SwingUtilities;
import java.util.Scanner;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_highgui.CvCapture;
import static com.googlecode.javacv.cpp.opencv_highgui.*;
public class Main extends JPanel{
/////////////////Initializes variables needed to read the image//////////////////////////////
BufferedImage img;
static int width;
static int height;
boolean image = false;
boolean video = false;
Scanner sc = new Scanner(System.in);
String url = sc.nextLine();
public Main(){
if(url.substring(url.length()-3).equals("jpg")){
image = true;
}
else{
video = true;
}
//setSize(5,5);
setVisible(true);
///////////////Calls loadImage method with the image URL/////////////////////////////////
if(image == true){
loadImage(url);
}
else{
System.out.println("yes");
printVideo();
}
}
/////////////////Attempts to read the image from the attached files and gets its width and height/////////////////////////
public void loadImage(String URL){
try{
img = ImageIO.read(Main.class.getResource(URL));
width = img.getWidth();
height = img.getHeight();
System.out.println("Image Read complete.");
}
catch(IOException e){
e.printStackTrace();
}
}
/*
static final int PIXEL_SIZE = 20;
static final int SCREEN_WIDTH = width;
static final int SCREEN_HEIGHT = height;
static final int ALL_PIXELS = (SCREEN_WIDTH*SCREEN_HEIGHT)/PIXEL_SIZE;
*/
//////////////GraphicsEngine that calls the printImage with graphics capabilities///////////////////////////////////
protected void paintComponent(Graphics g){
super.paintComponent(g);
//g.drawImage(img,0,0,width,height,this);
//System.out.println("Print complete.");
//draw(g,rgb,x,y);
if(image == true){
printImage(g);
}
//draw(g,rgb,x,y);
//System.out.println("Print complete.");
}
////////////Initializes variables based on width and height of image/////////////////////////////////
static final int PIXEL_SIZE = 1;
static int SCREEN_WIDTH = width;
static int SCREEN_HEIGHT = height;
static int ALL_PIXELS = (SCREEN_WIDTH*SCREEN_HEIGHT)/PIXEL_SIZE;
/*
public void draw(Graphics g, int[] rgb1, int x1, int y1){
//if the game is on draw these lines that make up the grid
//super.draw(g);
g.setColor(Color.RED);
g.fillRect(x,y,10,10);
System.out.println("YEEHAW");
//g.drawLine(i*PIXEL_SIZE, 0, i*PIXEL_SIZE, SCREEN_HEIGHT);
//g.drawLine(0,i*PIXEL_SIZE, SCREEN_WIDTH, i*PIXEL_SIZE);
}
*/
//////////////////Method to put RGB colors in an array//////////////////////////////////
int[] rgb = new int[3];
public int[] getActualRGB(int color){
return new int[]{
(color & 0xff0000) >> 16,
(color & 0xff00) >> 8,
color & 0xff
};
}
/*
public void draw1(Graphics g,int[] rgb,int row, int column){
g.setColor(Color.red);
int x = column;
int y= row;
g.fillRect(x,y,1,1);
}
*/
public void printVideo(){
width = CV_CAP_PROP_FRAME_WIDTH;
height = CV_CAP_PROP_FRAME_HEIGHT;
CvCapture capture = cvCreateFileCapture(url);
IplImage frame;
cvNamedWindow("Video",CV_WINDOW_AUTOSIZE);
for(;;){
frame = cvQueryFrame(capture);
if(frame == null){
System.out.println("ERROR: NO Video File");
break;
}
cvShowImage("Video",frame);
char c = (char) cvWaitKey(30);
if(c==27){
break;
}
}
cvReleaseCapture(capture);
cvDestroyWindow("Video");
}
/////////////////Method that goes through every pixel (is able to get the average color is neccessary) and finds its color and prints out a sqaure based on the color///////////////////////////
public void printImage(Graphics g){
//EXTERNAL
int SQUARELENGTH = 1;
for (int row = 0; row < SCREEN_HEIGHT; row += SQUARELENGTH){
// EVERY COLUMN
for (int col = 0; col < SCREEN_WIDTH; col+=SQUARELENGTH){
long average = 0;
int denominator = 0;
// INTERNAL
for (int row1 = 0; row1 < SQUARELENGTH; row1++){
for (int col1 = 0; col1 < SQUARELENGTH; col1++){
try{
if (!(row+row1>=SCREEN_HEIGHT) && !(col+col1>=SCREEN_WIDTH)){
average+= img.getRGB( col+col1, row+row1);
denominator+=1;
}
} catch (Exception e){
System.out.println("ERROR:\n"+SCREEN_HEIGHT+"\n"+SCREEN_WIDTH+"\n"+(row+row1)+"\n" + (col+col1) + "\n=======");
}
}
}
rgb = getActualRGB((int)average);
int grays = (rgb[0] + rgb[1] + rgb[2])/3;
//This is in color ====== g.setColor(new Color(rgb[0],rgb[1],rgb[2]));
g.setColor(new Color(grays,grays,grays));
/*
if(grays>100){
g.setColor(Color.WHITE);
System.out.print(" ");
}
else{
g.setColor(Color.BLACK);
System.out.print("■");
}
*/
g.fillRect(col,row,1,1);
/*
if (rgb[0] >= 125 && rgb[1] >= 125 && rgb[2] >= 1) System.out.print(" ");
else {System.out.print("#");}
// System.out.println("Average NOW: " + average);
*/
}
//System.out.println();
}
}
////////////Main method that adds all componenets to JFrame and prints out image///////////////////
public static void main(String[] args){
System.out.println("Image or Video URL: ");
JFrame frm = new JFrame();
frm.setVisible(true);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frm.add(new Main());
frm.setSize(width,height);
SCREEN_WIDTH = width;
SCREEN_HEIGHT = height;
//System.out.println(SCREEN_WIDTH);
//System.out.println(SCREEN_HEIGHT);
ALL_PIXELS = (SCREEN_WIDTH*SCREEN_HEIGHT)/PIXEL_SIZE;
}
}