@Override protected Void doInBackground(final Void... unused) { Wrapper wrapper = new Wrapper(); try { serverAddress = InetAddress.getByName(Constants.SERVER_IP); serverSocket = new Socket(); serverSocket.connect( new InetSocketAddress(Constants.SERVER_IP, Constants.SERVER_PORT), 5000); } catch (Exception e) { e.printStackTrace(); } wrapper.type = 0; wrapper.status = serverSocket.isConnected(); publishProgress(wrapper); try { Thread.sleep(500); dataInputStream = new DataInputStream(serverSocket.getInputStream()); dataOutputStream = new DataOutputStream(serverSocket.getOutputStream()); wrapper.type = 1; while (serverSocket.isConnected()) { bytes = 0; size = dataInputStream.readInt(); data = new byte[size]; for (int i = 0; i < size; i += bytes) { bytes = dataInputStream.read(data, i, size - i); } buff = new Mat(1, size, CvType.CV_8UC1); buff.put(0, 0, data); rev = Highgui.imdecode(buff, Highgui.CV_LOAD_IMAGE_UNCHANGED); Imgproc.cvtColor(rev, ret, Imgproc.COLOR_RGB2BGR); wrapper.img = ret; publishProgress(wrapper); Thread.sleep(75); } } catch (Exception e) { e.printStackTrace(); } return null; }
protected Mat ImageBGRFromString(byte[] data) { Mat frame = new Mat(1, data.length, CvType.CV_8UC1); frame.put(0, 0, data); return Highgui.imdecode(frame, 1); }