Skip to content

Commit e23476d

Browse files
author
Jianbin Qi
committed
processing android capture lib code
0 parents  commit e23476d

File tree

4 files changed

+145
-0
lines changed

4 files changed

+145
-0
lines changed

.classpath

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="lib" path="lib/core.jar"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>ProcessingAndroidCaptureLib</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package com.onlylemi.processing.android.capture;
2+
3+
import java.awt.image.BufferedImage;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.net.ServerSocket;
7+
import java.net.Socket;
8+
9+
import javax.imageio.ImageIO;
10+
11+
import processing.core.PImage;
12+
13+
/**
14+
* 手机摄像头捕获图像
15+
*
16+
* @author only乐秘
17+
*
18+
*/
19+
public class PhoneCapturer implements Runnable {
20+
21+
private ServerSocket ss = null;
22+
private BufferedImage image;
23+
private InputStream ins;
24+
private PImage pImage;
25+
private int color;
26+
27+
private int width, height;
28+
private long imageFrameRate;
29+
30+
private Thread thread;
31+
32+
private long start, end;
33+
34+
private boolean flag;
35+
36+
/**
37+
* 手机摄像头捕获图像构造函数
38+
*
39+
* @param width
40+
* 获取图像的宽
41+
* @param height
42+
* 获取图像的高
43+
* @param imageFrameRate
44+
* 刷新速率
45+
*/
46+
public PhoneCapturer(int width, int height, long imageFrameRate) {
47+
this.width = width;
48+
this.height = height;
49+
this.imageFrameRate = imageFrameRate;
50+
51+
flag = true;
52+
start = System.currentTimeMillis();
53+
try {
54+
ss = new ServerSocket(6000);
55+
} catch (IOException e) {
56+
e.printStackTrace();
57+
}
58+
59+
pImage = new PImage(width, height, PImage.RGB);
60+
61+
thread = new Thread(this);
62+
}
63+
64+
@Override
65+
public void run() {
66+
while (flag) {
67+
Socket s;
68+
try {
69+
s = ss.accept();
70+
if (s == null) {
71+
System.out.println("connect fail!!! ");
72+
flag = false;
73+
}
74+
// System.out.println("connect success. ");
75+
end = System.currentTimeMillis();
76+
ins = s.getInputStream();
77+
image = ImageIO.read(ins);
78+
79+
start = end;
80+
if (end - start < imageFrameRate) {
81+
Thread.sleep(imageFrameRate - (end - start));
82+
start = end;
83+
}
84+
ins.close();
85+
} catch (IOException e) {
86+
e.printStackTrace();
87+
} catch (Exception e) {
88+
e.printStackTrace();
89+
}
90+
}
91+
}
92+
93+
/**
94+
* 得到手机摄像头的图像
95+
*
96+
* @return
97+
*/
98+
public PImage getPImage() {
99+
if (image != null) {
100+
pImage = new PImage(image);
101+
pImage.resize(width, height);
102+
}
103+
return pImage;
104+
}
105+
106+
/**
107+
* 得到手机所识别的颜色
108+
*
109+
* @return
110+
*/
111+
public int getColor() {
112+
color = image.getRGB(image.getWidth() / 2, image.getHeight() / 2);
113+
return color;
114+
}
115+
116+
public void start() {
117+
thread.start();
118+
}
119+
120+
}

0 commit comments

Comments
 (0)