PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/VideoEmulation/src/org/siprop/opencv/SocketCamera.java

http://github.com/billmccord/OpenCV-Android
Java | 94 lines | 44 code | 15 blank | 35 comment | 0 complexity | 83188fa1499c7427bf1b6a596eea8f9c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /*
  2. * OpenCV for Android NDK
  3. * Copyright (c) 2006-2009 SIProp Project http://www.siprop.org/
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * In no event will the authors be held liable for any damages arising from the use of this software.
  7. * Permission is granted to anyone to use this software for any purpose,
  8. * including commercial applications, and to alter it and redistribute it freely,
  9. * subject to the following restrictions:
  10. *
  11. * 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  12. * 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  13. * 3. This notice may not be removed or altered from any source distribution.
  14. *
  15. */
  16. package org.siprop.opencv;
  17. import android.graphics.Bitmap;
  18. import android.graphics.BitmapFactory;
  19. import android.util.Log;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.net.InetSocketAddress;
  23. import java.net.Socket;
  24. /**
  25. * This simple socket camera reads images from a socket connection. It is
  26. * assumed that the server providing the socket is streaming video from an
  27. * actual camera.
  28. *
  29. * @author Bill McCord
  30. */
  31. public class SocketCamera {
  32. private static final String TAG = "SocketCamera";
  33. private static final int SOCKET_TIMEOUT = 1000;
  34. private final String mAddress;
  35. private final int mPort;
  36. /**
  37. * Create a new SocketCamera.
  38. *
  39. * @param address the ip address of the camera server
  40. * @param port the port that the camera server has opened a socket on
  41. */
  42. public SocketCamera(String address, int port) {
  43. Log.d(TAG, "instance");
  44. mAddress = address;
  45. mPort = port;
  46. }
  47. /**
  48. * Capture a bitmap from the camera server over a socket.
  49. *
  50. * @return the bitmap captured
  51. */
  52. public Bitmap captureBitmap() {
  53. Log.d(TAG, "Start captureBitmap.");
  54. Socket socket = null;
  55. Bitmap bitmap = null;
  56. try {
  57. Log.d(TAG, "Making socket connection");
  58. socket = new Socket();
  59. socket.bind(null);
  60. socket.setSoTimeout(SOCKET_TIMEOUT);
  61. socket.connect(new InetSocketAddress(mAddress, mPort), SOCKET_TIMEOUT);
  62. // obtain the bitmap
  63. Log.d(TAG, "Getting InputStream");
  64. InputStream in = socket.getInputStream();
  65. Log.d(TAG, "Decode bitmap");
  66. bitmap = BitmapFactory.decodeStream(in);
  67. } catch (IOException e) {
  68. Log.i(TAG, "Failed to obtain image over network", e);
  69. } finally {
  70. try {
  71. socket.close();
  72. } catch (IOException e) {
  73. /* ignore */
  74. }
  75. }
  76. Log.d(TAG, "End captureBitmap");
  77. return bitmap;
  78. }
  79. }