⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java2dtools.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        //cat.debug("-----------------------------------------------");
        //cat.debug("Input component: " + returnValue.toString() + "Ratio: " + ratioComponent);
        //cat.debug("Input win : " + window1.toString() + "Ratio: " + ratioWindows);
        try {
            // ratio from width to heigth

            boolean hasShrinked = false;
            // shrink component width if window is smaller
            if (returnValue.getWidth() > window1.getWidth()) {
                double newWidth = (window1.getWidth());
                double newHeigh = (newWidth / ratioComponent);

                returnValue.setRect(returnValue.getX(), returnValue.getY(), newWidth, newHeigh);
                hasShrinked = true;
                //cat.debug("CORRECT width: ratio: " + (newWidth / (float) newHeigh) + ", new w/h: " + newWidth + ", " + newHeigh);
            }

            // shrink frame heigh if window is too small
            // the wide of the image fits already to the windows bounds
            if (returnValue.getHeight() > window1.getHeight()) {
                double newHeigh = window1.getHeight();
                double newWidth = (newHeigh * ratioComponent);


                returnValue.setRect(returnValue.getX(), returnValue.getY(), newWidth, newHeigh);
                hasShrinked = true;
                //cat.debug("CORRECT Heigh: ratio: " + (newWidth / (float) newHeigh) + ", new w/h: " + newWidth + ", " + newHeigh);
            }

            if (!hasShrinked) {
                // strech frame width if window is too big
                double rateWithBigger = (window1.getWidth() / (double) (returnValue.getWidth()));
                //float rateWithBigger = 1000;
                double rateHeighBigger = (window1.getHeight() / (double) (returnValue.getHeight()));
                //float rateHeighBigger = 1000;

                double rateBigger = 1;
                double ratioFrameW = 1;
                double ratioFrameH = 1;
                if (rateWithBigger < rateHeighBigger) {
                    rateBigger = rateWithBigger;
                    //ratioFrameH = 1/ratioComponent;
                } else {
                    rateBigger = rateHeighBigger;
                    //ratioFrameW = ratioComponent;
                }

                double newWidth = (returnValue.getWidth() * rateBigger * ratioFrameW);
                double newHeigh = (returnValue.getHeight() * rateBigger * ratioFrameH);
                returnValue.setRect(returnValue.getX(), returnValue.getY(), newWidth, newHeigh);
                //cat.debug("CORRECT Streched: rW: " + ratioFrameW + ", rH: " + ratioFrameH + ", bigger rate: " + rateBigger);
                //cat.debug("-->New Width: " + newWidth + ", new Heigh: " + newHeigh);
            }  // end if strech

            // finally center the frame
            //int middleX = (int) window1.getWidth() / 2;
            //int middleY = (int) window1.getHeight() / 2;

            double offsetX = ((window1.getWidth() - returnValue.getWidth()) / 2);
            double offsetY = ((window1.getHeight() - returnValue.getHeight()) / 2);

            double with = returnValue.getWidth();
            double height = returnValue.getHeight();
            returnValue.setRect(returnValue.getX() + offsetX, returnValue.getY() + offsetY, with, height);
            //cat.debug("New Center: " + returnValue.toString());
            //returnValue.setBounds(0, 0, with, height);

        } catch (Exception e) {
            cat.error(e);
        }


        return returnValue;
    }


    /**
     * norimalize to 0..1
     *
     * @param pointList
     * @return Point2D.Double[]
     */
    public java.util.List normalizeCoordinate(java.util.List pointList) {
        if (pointList == null) {
            return new Vector(0, 1);
        }

        java.util.List returnValue = new Vector(pointList.size(), 5);

        Rectangle2D.Double border;
        border = (Rectangle2D.Double) calculateBorder(pointList, 5, new Rectangle2D.Double(0, 0, 0, 0));
        Rectangle2D.Double normalBorder =
                (Rectangle2D.Double) fitToWindow(new Rectangle2D.Double(0, 0, 1, 1), new Rectangle2D.Double(border.getX(), border.getY(), border.getWidth(), border.getHeight()));
        if (pointList == null) {
            return returnValue;
        }

        for (int i = 0; i < pointList.size(); i++) {
            Object o = (Object) pointList.get(i);
            Point2D.Double newPoint = null;
            if (Point.class.isInstance(o)) {
                Point oldPoint = (Point) o;
                newPoint = new Point2D.Double(oldPoint.getX(), oldPoint.getY());
            } else {
                newPoint = (Point2D.Double) o;
            }

            // add the points
            Point2D.Double normalPoint = (Point2D.Double) fitPointToOtherBorder(newPoint, border, normalBorder);
            returnValue.add(normalPoint);
        } // end for

        return returnValue;
    }


    // This method returns true if the specified image has transparent pixels
    public 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();

        if (cm == null) {
            return false;
        } else {
            return cm.hasAlpha();
        }
    }

    // This method returns a buffered image with the contents of an image
    public BufferedImage imageToBufferedImage(Image image) {
        if (image == null) {
            return null;
        }

        if (image instanceof BufferedImage) {
            return (BufferedImage) image;
        }

        // This code ensures that all the pixels in the image are loaded
        image = new ImageIcon(image).getImage();

        //System.out.println("image 1");
        //showImage(image);
        // Determine if the image has transparent pixels; for this method's
        // implementation, see e665 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);

            //   System.out.println("image 2");
            //showImage(bimage);
        } catch (HeadlessException e) {
            cat.error(e);
        }

        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, Color.WHITE, null);
        g.dispose();

        //System.out.println("image 3");
        //showImage(bimage);

        return bimage;
    }


    public Image bufferedImageToImage(BufferedImage bImage1) {
        Image returnValue = null;
        if (bImage1 == null) {
            return returnValue;
        }


        try {
            returnValue = Toolkit.getDefaultToolkit().createImage(bImage1.getSource());

            /*ImageFilter filter = new ImageFilter();
                JLabel i = new JLabel();
                Image img;
                img = i.createImage(new FilteredImageSource(img1.getSource(), filter));
            */
        } catch (Exception e) {
            cat.error(e);
        }

        return returnValue;
    }

    // old version, uses Filters. This means performance problems, because
    // the filter is applied each time the image has to be drawn.
/*public BufferedImage getBrighterImage(BufferedImage image1) {
       BufferedImage returnValue = image1;

        if (image1 == null) {
            return returnValue;
        }

       ImageFilter filter;
       filter = new Transparent(30);
       Image image;

        ImageProducer orgImageProducer = image1.getSource();
        ImageProducer imageProducer = new FilteredImageSource(orgImageProducer, filter);

        image = Toolkit.getDefaultToolkit().createImage(imageProducer);
       returnValue = ImageToBufferedImage(image);

       return returnValue;
    }*/

    public BufferedImage getBrighterImage(BufferedImage image1) {
        BufferedImage returnValue = image1;

        if (image1 == null) {
            return returnValue;
        }

        BufferedImage l_img = new BufferedImage(image1.getWidth(), image1.getHeight(),
                BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D l_gfx = l_img.createGraphics();
        l_gfx.setColor(new Color(1f, 1f, 1f));
        l_gfx.fillRect(0, 0, l_img.getWidth(), l_img.getHeight());

        l_gfx.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
        l_gfx.drawImage(image1, 0, 0, null);

        returnValue = l_img;

        return returnValue;
    }


    public void showImage(BufferedImage img1) {
        try {
            ImageFilter filter = new ImageFilter();
            JLabel i = new JLabel();
            Image img;
            //img = i.createImage(new FilteredImageSource(image1.getSource(), filter));
            img = i.createImage(new FilteredImageSource(img1.getSource(), filter));
            showImage(img);
        } catch (Exception e) {
            cat.error(e);
        }
    }

    public void showImage(Image img) {

        try {
            JDialog test = new JDialog(new JDialog(), true);
            int width = img.getWidth(null);
            int height = img.getHeight(null);
            if (width < 50) {
                width = 50;
            }
            if (height < 50) {
                height = 50;
            }
            test.setSize(width, height);
            test.getContentPane().setLayout(new BorderLayout());
            JButton x = new JButton("Image");
            ImageIcon icon = new ImageIcon(img);
            x.setIcon(icon);
            test.getContentPane().add(x, BorderLayout.CENTER);
            test.setVisible(true);
            //test.show();
        } catch (Exception e) {
            cat.error(e);
        }
    }


    /**
     * @param filename        filename of the input-File
     * @param width           sesired with of the returned image
     * @param heigh           sesired hight of the returned image

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -