📄 networkeddevicemonitorcontrol.java
字号:
npx.printStackTrace(); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("URL is NULL!"); } setDeviceError("Bad URL"); notifyDeviceErrorListeners(); return null; } catch (ClassCastException ccx) { ccx.printStackTrace(); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("This is not a HttpURLConnection"); } setDeviceError("Incorrect URL connection type. Expected URL connection."); notifyDeviceErrorListeners(); return null; } catch (Exception x) { x.printStackTrace(); setDeviceError("General CamServer Error."); notifyDeviceErrorListeners(); return null; } if (camServer == null) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { //LOG.info ("authenticating"); } setDeviceError("CamServer Error."); notifyDeviceErrorListeners(); return null; } try { camServer.setAllowUserInteraction(false); camServer.setDoInput(true); camServer.setRequestMethod("GET"); } catch (ProtocolException px) { px.printStackTrace(); setDeviceError("CamServer protocol error: " + px.getMessage()); notifyDeviceErrorListeners(); return null; } catch (Exception x) { x.printStackTrace(); setDeviceError("CamServer General Error: " + x.getMessage()); notifyDeviceErrorListeners(); return null; } camServer.setRequestProperty("Keep-Alive", "300"); if (authenticate) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { // LOG.info ("authenticating"); } camServer.setRequestProperty("Authorization", "Basic " + getEncodedCredentials()); } else { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { //LOG.info ("NOT authenticating"); } } return camServer; } private void setMimeType(String mimeType) { this.mimeType = mimeType; } public String getMimeType() { return this.mimeType; } public String getResponseString() { return this.responseString; } public void setResponseString(String responseString) { this.responseString = responseString; } private ByteArrayOutputStream requestURLImage() { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { //LOG.info("requestURLImage"); } ByteArrayOutputStream imageByteArray = new ByteArrayOutputStream(); BufferedInputStream in = null; int contentLength = 0; HttpURLConnection camServer = getHttpConnection(getURL(getURLString()), isAuthenticated()); try { camServer.connect(); } catch (SocketTimeoutException stx) { stx.printStackTrace(); setDeviceError("CamServer Timeout Error"); notifyDeviceErrorListeners(); return null; } catch (IOException iox) { iox.printStackTrace(); setDeviceError("CamServer I/O Error."); notifyDeviceErrorListeners(); return null; } catch (Exception x) { x.printStackTrace(); setDeviceError("General CamServer Device Access Error."); notifyDeviceErrorListeners(); return null; } //dont do anythign more and return null if content length is nada or 1 try { in = new BufferedInputStream(camServer.getInputStream()); } catch (IOException iox) { iox.printStackTrace(); setDeviceError("CamServer I/O Error."); notifyDeviceErrorListeners(); return null; } try { setResponseString(camServer.getResponseMessage()); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { //LOG.info ("response "+getResponseString ()); } setMimeType(camServer.getHeaderField("Content-Type")); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { //LOG.info("mimeType "+getMimeType()); //LOG.info("content length "+camServer.getHeaderField ("Content-Length")); } String s = camServer.getHeaderField("Content-Length"); if (s != null) contentLength = Integer.valueOf(s).intValue(); } catch (IOException iox) { iox.printStackTrace(); setDeviceError("CamServer I/O Error"); notifyDeviceErrorListeners(); return null; } catch (NumberFormatException nfx) { nfx.printStackTrace(); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("Content Length not Parsable"); } setDeviceError("Content Length Not Reabable Error."); notifyDeviceErrorListeners(); return null; } // newline;CR i think // if (contentLength > 1) { contentLength -= 2; } //dont do anythign more and return null if content length is nada or 1 try { in = new BufferedInputStream(camServer.getInputStream()); } catch (IOException iox) { iox.printStackTrace(); setDeviceError("CamServer I/O Exception"); notifyDeviceErrorListeners(); return null; } int contentLengthCounter = contentLength; while (contentLengthCounter > 0) { int readLength = 0; byte[] byteBuffer = new byte[this.SERVER_CHUNK_READ_SIZE]; //1460 try { readLength = in.read(byteBuffer); } catch (IOException iox) { iox.printStackTrace(); setDeviceError("CamServer Data Read Error"); notifyDeviceErrorListeners(); return null; } if (readLength == -1) { contentLengthCounter = 0; } else { } imageByteArray.write(byteBuffer, 0, readLength); contentLengthCounter -= readLength; } return imageByteArray; } private void captureImage() { try { ByteArrayOutputStream imageByteArray = this.requestURLImage(); if (this.getTransmitState() == this.STARTED) { outgoingBuffer.addLast(imageByteArray); } ((MonitorComponent) this.getMonitorComponent()).newImage(new ByteArrayInputStream(imageByteArray.toByteArray())); if (this.averageImageEncodeTime != 0) { this.averageImageEncodeTime = (this.averageImageEncodeTime + this.imageEncodeTime) / 2; } } catch (Exception x) { x.printStackTrace(); setDeviceError("Capture Image Error."); notifyDeviceErrorListeners(); return; } if (bufferStarvedLock.isLocked()) { synchronized (bufferStarvedLock) { bufferStarvedLock.setLocked(false); bufferStarvedLock.notifyAll(); } } } public long getAverageImageEncodeTime() { return this.averageImageEncodeTime; } public void obtainHardware() { this.setMonitorComponent(new MonitorComponent()); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("obtainHardware"); } } public void startMonitorCapture() { this.setMonitorState(this.STARTED); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("startMonitorCapture"); } } public void startMonitor() { if (this.getMonitorState() == this.STOPPED) { this.start(); } this.setMonitorState(this.STARTED); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("startMonitor"); } } public void stopMonitor() { this.setMonitorState(this.STOPPED); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("stopMonitor"); } } public void startTransmit() { this.setTransmitState(this.STARTED); this.dispatchThread.start(); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("startTransmit"); } } public boolean isTransmit() { return transmit; } public void setTransmit(boolean trans) { transmit = trans; } public int getOutgoingBufferSize() { return outgoingBuffer.size(); } public int getMessagesSent() { return messagesSent; } public int getBytesSent() { return bytesSent; } public void releaseHardware() { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("releaseHardware"); } this.stopMonitor(); this.stopMonitorCapture(); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("Release Hardware"); } } public void stopMonitorCapture() { this.setMonitorState(this.STOPPED); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("stopMonitorCapture"); } } public void stopTransmit() { this.setTransmitState(this.STOPPED); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("stopTransmit"); } } public int getBufferSize() { return this.outgoingBuffer.size(); } public DialogMessage getNewTemplateMessage() { return (DialogMessage) this.templateMessage.clone(); } public int getImagesPerMessage() { return this.imagesPerMessage; } public void setImagesPerMessage(int imagesPerMessage) { if (imagesPerMessage < this.MINIMUM_IMAGES_PER_MESSAGE) { this.imagesPerMessage = this.MINIMUM_IMAGES_PER_MESSAGE; } else { this.imagesPerMessage = imagesPerMessage; } } /** * Statistical Accessor. Returns the time delta of encoding an image */ public long getImageEncodeTime() { return this.imageEncodeTime; } public void setImageCompression(int imageCompression) { if (imageCompression >= this.MINIMUM_IMAGE_COMPRESSION) { this.imageCompression = imageCompression; } else { this.imageCompression = this.MINIMUM_IMAGE_COMPRESSION; } } public int getImageCompression() { return imageCompression; } private int getMonitorState() { return this.monitorState; } private void setMonitorState(int monitorState) { this.monitorState = monitorState; } class MyLock extends Object { private boolean locked = false; public boolean isLocked() { return locked; } public void setLocked(boolean locked) { this.locked = locked; } } public Component getMonitorComponent() { return this.monitorComponent; } private void setMonitorComponent(MonitorComponent monitorComponent) { this.monitorComponent = monitorComponent; } class MonitorComponent extends JComponent {//Canvas { Dimension size = null; ImageReader reader = null; Image image = null; public MonitorComponent() { super(); Iterator readers = ImageIO.getImageReadersByFormatName("jpeg"); reader = (ImageReader) readers.next(); } public Dimension getPreferredSize() { return size == null ? new Dimension(0, 0) : size; } private void setImageSize(Dimension size) { this.size = size; this.setSize(size); if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { LOG.info("imageSize " + size); } } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; //super.paintComponent (g); g2.drawImage(image, 0, 0, size.width, size.height, null); //g.drawImage (image,0,0,null); g2.dispose(); } public void newImage(ByteArrayInputStream encodedImageStream) { if (Logging.SHOW_INFO && LOG.isLoggable(Level.INFO)) { //LOG.info("newImage"); } try { long in = System.currentTimeMillis(); // this is just a impl of ImageInputStreamImpl.. we can subclass // later and make our own MemoryCacheImageInputStream imageStream = new MemoryCacheImageInputStream(encodedImageStream); reader.setInput(imageStream); BufferedImage bufferedImage = reader.read(0); if (size == null) { setImageSize(new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight())); } long out = System.currentTimeMillis(); imageEncodeTime = out - in; in = System.currentTimeMillis(); image = bufferedImage; //maybe imageUpdate??? repaint(); // Copy image to buffered image. /* Graphics g = this.getGraphics (); g.drawImage (bufferedImage, 0, 0, null); g.dispose (); */ out = System.currentTimeMillis(); } catch (Exception x) { x.printStackTrace(); setDeviceError("Image Capture Error."); notifyDeviceErrorListeners(); return; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -