?? jwebcamplayer.java
字號:
//r = in.read(buffer, n, HDLREN + siz - n); r = in.read(buffer, n, buffer.length - n); } catch(Exception e) { e.printStackTrace(); } n += r; if (DEBUGGING) System.out.println(" chunk read, size="+Integer.toString(r)+" total="+Integer.toString(n)); /*if (r<=0) { n = r; break; }*/ } while(n<(siz+HDRLEN)); } if (DEBUGGING) System.out.println("jpeg read, size="+Integer.toString(n)); } byte [] buffer2 = new byte [n]; for(int i=0; i<n; i++) buffer2[i] = buffer[i+HDRLEN]; try{ BufferedImage image = ImageIO.read(new ByteArrayInputStream(buffer2)); if(do_overlay) { Graphics2D g = image.createGraphics(); g.drawImage(image, 0, 0, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.4f)); g.drawImage(overlay, 0, 0, null); g.dispose(); } ImageIcon ii = new ImageIcon(image);// ImageIcon ii = new ImageIcon(Toolkit.getDefaultToolkit().createImage(buffer2)); if (ii.getIconHeight()<=1 || ii.getIconWidth()<=1) { if (DEBUGGING) System.out.println("--invalid image got"); continue; } else { if (DEBUGGING) System.out.println("--VALID image got"); } m_label.setIcon(ii); } catch (IOException e) {} }//while try { connection.close(); // (Alternatively, you might depend on the server // to close the connection.) } catch (IOException e) { } m_label.setIcon(null); m_label.setText("JWebcamPlayer - Connection closed"); if (DEBUGGING) { if (m_stop) System.out.println("** worker ending (m_stop==true)"); else System.out.println("** worker ending (m_stop==false)"); } m_stop = true; return null; } }; m_worker.start(); //required for SwingWorker 3 } public void stop() { if (DEBUGGING) System.out.println("--stopping worker (applet stop)"); m_stop = true; } // This method returns true if the specified image has transparent pixels public static boolean hasAlpha(Image image) { // If buffered image, the color model is readily available if (image instanceof BufferedImage) { BufferedImage bimage = (BufferedImage)image; return bimage.getColorModel().hasAlpha(); } // Use a pixel grabber to retrieve the image's color model; // grabbing a single pixel is usually sufficient PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false); try { pg.grabPixels(); } catch (InterruptedException e) { } // Get the image's color model ColorModel cm = pg.getColorModel(); return cm.hasAlpha(); } // This method returns a buffered image with the contents of an image public static BufferedImage toBufferedImage(Image image) { if (image instanceof BufferedImage) { return (BufferedImage)image; } // This code ensures that all the pixels in the image are loaded image = new ImageIcon(image).getImage(); // Determine if the image has transparent pixels; for this method's // implementation, see e661 Determining If an Image Has Transparent Pixels boolean hasAlpha = hasAlpha(image); // Create a buffered image with a format that's compatible with the screen BufferedImage bimage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); try { // Determine the type of transparency of the new buffered image int transparency = Transparency.OPAQUE; if (hasAlpha) { transparency = Transparency.BITMASK; } // Create the buffered image GraphicsDevice gs = ge.getDefaultScreenDevice(); GraphicsConfiguration gc = gs.getDefaultConfiguration(); bimage = gc.createCompatibleImage( image.getWidth(null), image.getHeight(null), transparency); } catch (HeadlessException e) { // The system does not have a screen } if (bimage == null) { // Create a buffered image using the default color model int type = BufferedImage.TYPE_INT_RGB; if (hasAlpha) { type = BufferedImage.TYPE_INT_ARGB; } bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type); } // Copy image to buffered image Graphics g = bimage.createGraphics(); // Paint the image onto the buffered image g.drawImage(image, 0, 0, null); g.dispose(); return bimage; } public void mouseClicked(MouseEvent evt) { int x = evt.getX(); // Location where user clicked. int y = evt.getY(); System.out.println("x="+Integer.toString(x)+" y="+Integer.toString(y)); if(do_overlay) { //base check if(x>0 && x<42 && y>0 && y<42) { if(y>2 && y<18) { //contrast if(x>0 && x<10) { //decrement System.out.println("c -"); contrast_down = true; } else if(x > 30 && x < 40) { //increment System.out.println("c +"); contrast_up = true; } } else if(y > 22 && y < 40) { //brightness if(x>0 && x<10) { //decrement System.out.println("b -"); bright_down = true; } else if(x > 30 && x < 40) { //increment System.out.println("b +"); bright_up = true; } } } else { do_overlay=false; } } else { do_overlay=true; } } public void mousePressed(MouseEvent evt) { } public void mouseReleased(MouseEvent evt) { } public void mouseDragged(MouseEvent evt) { } public void mouseMoved(MouseEvent evt) { } public void mouseEntered(MouseEvent evt) { } public void mouseExited(MouseEvent evt) { } private final int HDRLEN = 50; private final int SZOFS = 29; private BufferedImage overlay = null; private boolean do_overlay = false; private JLabel m_label = null; private String m_strServer = null; private String m_strPort = null; private String m_strColor = null; private boolean m_stop = false; private SwingWorker m_worker = null; private boolean bright_up = false; private boolean bright_down = false; private boolean contrast_up = false; private boolean contrast_down = false; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -