Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.41 KB | None | 0 0
  1. com.sun.image.codec.jpeg.ImageFormatException: Not a JPEG file: starts with 0x0d 0x0a
  2. at sun.awt.image.codec.JPEGImageDecoderImpl.readJPEGStream(Native Method)
  3. at sun.awt.image.codec.JPEGImageDecoderImpl.decodeAsBufferedImage(Unknown Source)
  4. at test.AxisCamera1.readJPG(AxisCamera1.java:130)
  5. at test.AxisCamera1.readMJPGStream(AxisCamera1.java:121)
  6. at test.AxisCamera1.readStream(AxisCamera1.java:100)
  7. at test.AxisCamera1.run(AxisCamera1.java:171)
  8. at java.lang.Thread.run(Unknown Source)
  9.  
  10. private static final long serialVersionUID = 1L;
  11. public boolean useMJPGStream = true;
  12. public String jpgURL = "http://ip here/video.cgi/jpg/image.cgi?resolution=640×480";
  13. public String mjpgURL = "http://ip here /video.cgi/mjpg/video.cgi?resolution=640×480";
  14. DataInputStream dis;
  15. private BufferedImage image = null;
  16. public Dimension imageSize = null;
  17. public boolean connected = false;
  18. private boolean initCompleted = false;
  19. HttpURLConnection huc = null;
  20. Component parent;
  21.  
  22. /** Creates a new instance of AxisCamera */
  23. public AxisCamera1(Component parent_) {
  24. parent = parent_;
  25. }
  26.  
  27. public void connect() {
  28. try {
  29. URL u = new URL(useMJPGStream ? mjpgURL : jpgURL);
  30. huc = (HttpURLConnection) u.openConnection();
  31.  
  32. // System.out.println(huc.getContentType());
  33. InputStream is = huc.getInputStream();
  34.  
  35. connected = true;
  36. BufferedInputStream bis = new BufferedInputStream(is);
  37. dis = new DataInputStream(bis);
  38. if (!initCompleted)
  39. initDisplay();
  40. } catch (IOException e) { // incase no connection exists wait and try
  41. // again, instead of printing the error
  42. try {
  43. huc.disconnect();
  44. Thread.sleep(60);
  45. } catch (InterruptedException ie) {
  46. huc.disconnect();
  47. connect();
  48. }
  49. connect();
  50. } catch (Exception e) {
  51. ;
  52. }
  53. }
  54.  
  55. public void initDisplay() { // setup the display
  56. if (useMJPGStream)
  57. readMJPGStream();
  58. else {
  59. readJPG();
  60. disconnect();
  61. }
  62. imageSize = new Dimension(image.getWidth(this), image.getHeight(this));
  63. setPreferredSize(imageSize);
  64. parent.setSize(imageSize);
  65. parent.validate();
  66. initCompleted = true;
  67. }
  68.  
  69. public void disconnect() {
  70. try {
  71. if (connected) {
  72. dis.close();
  73. connected = false;
  74. }
  75. } catch (Exception e) {
  76. ;
  77. }
  78. }
  79.  
  80. public void paint(Graphics g) { // used to set the image on the panel
  81. if (image != null)
  82. g.drawImage(image, 0, 0, this);
  83. }
  84.  
  85. public void readStream() { // the basic method to continuously read the
  86. // stream
  87. try {
  88. if (useMJPGStream) {
  89. while (true) {
  90.  
  91. readMJPGStream();
  92. parent.repaint();
  93. }
  94. } else {
  95. while (true) {
  96. connect();
  97. readJPG();
  98. parent.repaint();
  99. disconnect();
  100.  
  101. }
  102. }
  103.  
  104. } catch (Exception e) {
  105. ;
  106. }
  107. }
  108.  
  109. public void readMJPGStream() { // preprocess the mjpg stream to remove the
  110. // mjpg encapsulation
  111. readLine(3, dis); // discard the first 3 lines
  112. readJPG();
  113. readLine(2, dis); // discard the last two lines
  114. }
  115.  
  116. public void readJPG() { // read the embedded jpeg image
  117. try {
  118.  
  119.  
  120. JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(dis);
  121. image = decoder.decodeAsBufferedImage();
  122.  
  123. } catch (Exception e) {
  124. e.printStackTrace();
  125. disconnect();
  126. }
  127.  
  128. }
  129.  
  130. public void readLine(int n, DataInputStream dis) { // used to strip out the
  131. // header lines
  132. for (int i = 0; i < n; i++) {
  133. readLine(dis);
  134. }
  135. }
  136.  
  137. public void readLine(DataInputStream dis) {
  138. try {
  139. boolean end = false;
  140. String lineEnd = "n"; // assumes that the end of the line is marked
  141. // with this
  142. byte[] lineEndBytes = lineEnd.getBytes();
  143. byte[] byteBuf = new byte[lineEndBytes.length];
  144.  
  145. while (!end) {
  146. dis.read(byteBuf, 0, lineEndBytes.length);
  147. String t = new String(byteBuf);
  148. System.out.print(t); // uncomment if you want to see what the
  149. // lines actually look like
  150. if (t.equals(lineEnd))
  151. end = true;
  152. }
  153. } catch (Exception e) {
  154. e.printStackTrace();
  155. }
  156.  
  157. }
  158.  
  159. public void run() {
  160. System.out.println("in Run...................");
  161. connect();
  162. readStream();
  163. }
  164.  
  165. @SuppressWarnings("deprecation")
  166. public static void main(String[] args) {
  167.  
  168.  
  169. JFrame jframe = new JFrame();
  170. jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  171. AxisCamera1 axPanel = new AxisCamera1(jframe);
  172. new Thread(axPanel).start();
  173. jframe.getContentPane().add(axPanel);
  174. jframe.pack();
  175. jframe.show();
  176. }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement