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

📄 java2dtools.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
            Point2D down = (Point2D) typePoint.clone();
            down.setLocation(0, 0);

            boolean isFirst = true;
            for (Iterator it = pointList1.iterator(); it.hasNext();) {
                Point2D p = (Point2D) it.next();

                //cat.debug("DOT: " + p.toString());
                if (isFirst) {
                    left = p;
                    right = p;
                    up = p;
                    down = p;

                    isFirst = false;
                }
                if (left.getX() > p.getX()) left = p;
                if (right.getX() < p.getX()) right = p;
                if (up.getY() > p.getY()) up = p;
                if (down.getY() < p.getY()) down = p;
            } // end while

            int size = dotSize1;
            returnValue.setRect(left.getX() - size, up.getY() - size,
                    right.getX() - left.getX() + 2 * size, down.getY() - up.getY() + 2 * size);
            //cat.debug("New Surrounding border: " + returnValue.toString());
        } catch (Exception e) {
            e.printStackTrace();
            cat.error(e);
        }

        return returnValue;
    }

    /**
     * fit the point to a new rectangle size. Used to transform coordinates
     */
    public Point2D fitPointToOtherBorder(Point2D point1, Rectangle2D orginalBorder1, Rectangle2D newBorder1) {
        Point2D returnValue = point1;

        if (point1 == null || orginalBorder1 == null || newBorder1 == null) {
            return returnValue;
        }

        try {
            double ratioX = newBorder1.getWidth() / orginalBorder1.getWidth();
            double ratioY = newBorder1.getHeight() / orginalBorder1.getHeight();

            double relX = point1.getX() - orginalBorder1.getX();
            double relY = point1.getY() - orginalBorder1.getY();

            returnValue = (Point2D) point1.clone();
            returnValue.setLocation((newBorder1.getX() + (relX * ratioX)),
                    (newBorder1.getY() + (relY * ratioY)));

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

        return returnValue;
    }

    /**
     * list of Point()
     */
    public java.util.List fitPointToOtherBorder(java.util.List pointList1, Rectangle orginalBorder1, Rectangle newBorder1) {
        if (pointList1 == null) {
            return new Vector();
        }
        java.util.List returnValue = new Vector(pointList1.size());

        try {
            for (Iterator listIterator = pointList1.iterator(); listIterator.hasNext();) {
                Point point = (Point) listIterator.next();
                Point newPoint = (Point) fitPointToOtherBorder(point, orginalBorder1, newBorder1);
                returnValue.add(newPoint);
            }
        } catch (Exception e) {
            cat.error(e);
        }

        return returnValue;
    }


    public byte[] intToByteArray(int value) {
        byte[] byte_array = new byte[4];

        byte_array[0] = (byte) ((value >>> 24) & 0xFF);
        byte_array[1] = (byte) ((value >>> 16) & 0xFF);
        byte_array[2] = (byte) ((value >>> 8) & 0xFF);
        byte_array[3] = (byte) (value & 0xFF);
        return byte_array;
    }

    public int byteArrayToInt(byte[] byte_array) {
        int value = ((((int) byte_array[0] & 0xFF) << 24) |
                (((int) byte_array[1] & 0xFF) << 16) |
                (((int) byte_array[2] & 0xFF) << 8) |
                ((int) byte_array[3] & 0xFF));
        return value;
    }

    /**
     * Grabs the pixels of the given Image object and returns the array of those
     * pixels. Note that the Image needs to be preloaded in order for this
     * method to work correctly.
     * Parameters:
     * Image img: The image whose pixels to grab.
     */
    public byte[] serializeImage(Image image1, int TYPE_CODING) {
        byte[] returnValue = new byte[0];
        if (image1 == null) {
            return returnValue;
        }

        try {
            switch (TYPE_CODING) {
                case TYPE_SERIALIZE_RAW:
                    {

                        int width = image1.getWidth(null);
                        int height = image1.getHeight(null);

                        // one int has four bytes
                        int[] pixels = new int[((width * height) + 2)];

                        PixelGrabber grabber = new PixelGrabber(image1, 0, 0, width, height, pixels, 0, width);
                        if (!grabber.grabPixels()) {
                            return returnValue;
                        }
                        pixels[pixels.length - 2] = width;
                        pixels[pixels.length - 1] = height;

                        // convert to a byte array
                        byte[] byteArray = new byte[pixels.length * 4];
                        for (int counter = 0; counter < pixels.length; counter++) {
                            int element = pixels[counter];

                            byte[] bytes = java2dTools.intToByteArray(element);
                            byteArray[counter * 4] = bytes[0];
                            byteArray[(counter * 4) + 1] = bytes[1];
                            byteArray[(counter * 4) + 2] = bytes[2];
                            byteArray[(counter * 4) + 3] = bytes[3];
                        }

                        returnValue = byteArray;
                        break;
                    }
                case TYPE_SERIALIZE_JPEG:
                    {
                        TYPE_SERIALIZE_JPEG:
                        returnValue = serializeImage(imageToBufferedImage(image1), TYPE_CODING);
                        break;
                    }
            } // end switch
        } catch (Exception e) {
            cat.error(e);
        }

        return returnValue;
    }

    public byte[] serializeImage(BufferedImage bImage1, int TYPE_CODING) {
        byte[] returnValue = new byte[0];
        try {
            switch (TYPE_CODING) {
                case TYPE_SERIALIZE_RAW:
                    {
                        returnValue = serializeImage(bufferedImageToImage(bImage1), TYPE_CODING);
                        break;
                    }
                case TYPE_SERIALIZE_JPEG:
                    {
                        ByteArrayOutputStream fos = new ByteArrayOutputStream();
                        JPEGImageEncoder encoder =
                                JPEGCodec.createJPEGEncoder(fos);
                        encoder.encode(bImage1);
                        fos.flush();
                        returnValue = fos.toByteArray();
                        fos.close();
                        break;
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
            cat.error(e);
        }

        return returnValue;
    }


    // Creates an Image object from the given array of pixels.
    // Note that the returned Image is created using an ImageProducer
    // (MemoryImageSource), and such Images are painted much slower than
    // offscreen Images created via Component.createImage(int,int) and you can't
    // draw on these Images as their getGraphics() method will return null. If
    // you need an Image that will be drawn fast and/or you need to paint
    // additional things on the Image, create an offscreen Image using
    // Component.createImage(int,int) and paint the Image returned by this method
    // on that Image.
    // Parameters:
    //   int [] pixels: The pixels array.
    public Image deserializeImage(byte[] bytePixels, Image default1, int TYPE_CODING) {
        Image returnValue = default1;

        try {
            switch (TYPE_CODING) {
                case TYPE_SERIALIZE_RAW:
                    {

                        // convert the byte array to an int array
                        int dataLength = bytePixels.length / 4;
                        int[] pixels = new int[dataLength];
                        byte[] bytes = new byte[4];
                        int counter = 0;
                        while (counter < dataLength) {
                            int byteCounter = counter * 4;

                            // String subString = str1.substring(counter, counter + 32);
                            //data[counter] = Integer.parseInt(subString);


                            bytes[0] = bytePixels[byteCounter];
                            bytes[1] = bytePixels[byteCounter + 1];
                            bytes[2] = bytePixels[byteCounter + 2];
                            bytes[3] = bytePixels[byteCounter + 3];

                            pixels[counter] = java2dTools.byteArrayToInt(bytes);
                            counter++;
                        }


                        if (pixels.length == 0) {
                            return returnValue;
                        }


                        int width = pixels[pixels.length - 2];
                        int height = pixels[pixels.length - 1];

                        //int[] imageData = new int[pixels.length - 2];
                        //System.arraycopy(imageData, 0, pixels, 2, pixels.length - 2);
                        MemoryImageSource imageSource = new MemoryImageSource(width, height, pixels, 0, width);

                        returnValue = Toolkit.getDefaultToolkit().createImage(imageSource);
                        break;
                    }
                case TYPE_SERIALIZE_JPEG:
                    returnValue = deserializeImage(bytePixels, imageToBufferedImage(default1), TYPE_CODING);
                    break;
            }


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

        return returnValue;
    }

    public BufferedImage deserializeImage(byte[] pixels, BufferedImage default1, int TYPE_CODING) {
        BufferedImage returnValue = default1;
        try {
            switch (TYPE_CODING) {
                case TYPE_SERIALIZE_RAW:
                    {
                        returnValue = imageToBufferedImage(deserializeImage(pixels, bufferedImageToImage(default1), TYPE_CODING));
                        break;
                    }
                case TYPE_SERIALIZE_JPEG:
                    {
                        InputStream fin = new ByteArrayInputStream(pixels);
                        returnValue = javax.imageio.ImageIO.read(fin);
                        fin.close();
                        break;
                    }
            }
        } catch (Exception e) {
            e.printStackTrace();
            cat.error(e);
        }

        return returnValue;
    }

    public BufferedImage javaObjectsToImage(IComponent[] componentArray) {
        BufferedImage returnValue = null;


        try {
            BufferedImage bffImg = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
            Graphics gfx = bffImg.createGraphics();
            gfx.setColor(Color.WHITE);
            gfx.fillRect(0, 0, (bffImg.getWidth() - 1), (bffImg.getHeight() - 1));
            //gfx.drawRect(10, 10, 50, 20);  // draw a rectangle

            for (int counter = 0; counter < componentArray.length; counter++) {
                IComponent iComponent = componentArray[counter];
                iComponent.paint(gfx);
            }

            //showImage(bffImg);
            returnValue = bffImg;
        } catch (Exception e) {
            cat.error(e);
            e.printStackTrace();
        }

        return returnValue;
    }


} // end class Java2dTools

⌨️ 快捷键说明

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