|
| 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