Animating multiple images using Java

2019-02-26 14:06发布

I have a question about animating java screen shots. My application is using the robot() function and a loop to take multiple screenshots and save them with names in consecutive orders, 1 to the loop number.

The thing is I need to put those images into a "movie", the format doesn't matter all that much though. Unfortunately I have no clue how to do this and I'm on my iPad as I'm traveling.

EDIT: What I mean is I need something that can create a movie or animation from my Java application with those screenshots.

标签: java image movie
3条回答
Ridiculous、
2楼-- · 2019-02-26 14:12

You could use any GIF creator out there to generate a Movie from Pictures. Do you need to do this on your ipad? maybe use this app

Or use a website like this

But what exactly does this have to do with Java?

EDIT: as posted in the comment, a Java Class to creata a GIF from images is given in this post

查看更多
冷血范
3楼-- · 2019-02-26 14:13

The (obsolete, abandoned) Java Media Framework can turn JPEGs into a MOV. Though note they have atrocious compression (using the particular compression codec implemented), and result in huge file sizes. An example can be seen in this answer.

查看更多
三岁会撩人
4楼-- · 2019-02-26 14:15

You can create an animated GIF without a third-party library like this:

void writeAnimatedGif(OutputStream stream,
                      Iterable<BufferedImage> frames,
                      int delayInMilliseconds,
                      Integer repeatCount)
throws IOException {
    try (ImageOutputStream iioStream =
            ImageIO.createImageOutputStream(stream)) {

        ImageWriter writer =
            ImageIO.getImageWritersByMIMEType("image/gif").next();
        writer.setOutput(iioStream);

        writer.prepareWriteSequence(null);

        for (BufferedImage frame : frames) {
            writeFrame(frame, delayInMilliseconds, writer, repeatCount);
            repeatCount = null;
        }

        writer.endWriteSequence();
        writer.dispose();
    }
}

void writeFrame(BufferedImage image,
                int delayInMilliseconds,
                ImageWriter writer,
                Integer repeatCount)
throws IOException {
    ImageTypeSpecifier type =
        ImageTypeSpecifier.createFromRenderedImage(image);
    IIOMetadata metadata = writer.getDefaultImageMetadata(type, null);
    String format = metadata.getNativeMetadataFormatName();

    Node tree = metadata.getAsTree(format);

    if (repeatCount != null)
    {
        setRepeatCount(repeatCount, tree);
    }

    setDelayTime(delayInMilliseconds, tree);

    metadata.setFromTree(format, tree);

    writer.writeToSequence(new IIOImage(image, null, metadata), null);
}

private void setRepeatCount(Number repeatCount, Node imageMetadata)
{
    Element root = (Element) imageMetadata;

    ByteBuffer buf = ByteBuffer.allocate(3).order(ByteOrder.LITTLE_ENDIAN);
    buf.put((byte) 1);  // sub-block index (always 1)
    byte[] appExtBytes = buf.putShort(repeatCount.shortValue()).array();

    Element appExtContainer;
    NodeList nodes = root.getElementsByTagName("ApplicationExtensions");
    if (nodes.getLength() > 0) {
        appExtContainer = (Element) nodes.item(0);
    } else {
        appExtContainer = new IIOMetadataNode("ApplicationExtensions");

        Node reference = null;
        nodes = root.getElementsByTagName("CommentExtensions");
        if (nodes.getLength() > 0) {
            reference = nodes.item(0);
        }

        root.insertBefore(appExtContainer, reference);
    }

    IIOMetadataNode appExt =
        new IIOMetadataNode("ApplicationExtension");
    appExt.setAttribute("applicationID", "NETSCAPE");
    appExt.setAttribute("authenticationCode", "2.0");
    appExt.setUserObject(appExtBytes);

    appExtContainer.appendChild(appExt);
}


private void setDelayTime(int delayInMilliseconds, Node imageMetadata)
{
    Element root = (Element) imageMetadata;

    Element gce;
    NodeList nodes = root.getElementsByTagName("GraphicControlExtension");
    if (nodes.getLength() > 0) {
        gce = (Element) nodes.item(0);
    } else {
        gce = new IIOMetadataNode("GraphicControlExtension");

        Node reference = null;
        nodes = root.getElementsByTagName("PlainTextExtension");
        if (nodes.getLength() > 0) {
            reference = nodes.item(0);
        }
        if (reference == null) {
            nodes = root.getElementsByTagName("ApplicationExtensions");
            if (nodes.getLength() > 0) {
                reference = nodes.item(0);
            }
        }
        if (reference == null) {
            nodes = root.getElementsByTagName("CommentExtensions");
            if (nodes.getLength() > 0) {
                reference = nodes.item(0);
            }
        }

        root.insertBefore(gce, reference);
    }

    gce.setAttribute("delayTime",
        String.valueOf(delayInMilliseconds / 10));
}

See also http://docs.oracle.com/javase/7/docs/api/javax/imageio/metadata/doc-files/gif_metadata.html#gif_image_metadata_format .

查看更多
登录 后发表回答