使用Java动画多个图像

时间:2013-10-28 12:02:15

标签: java image movie

我有一个关于动画java屏幕截图的问题。 我的应用程序使用robot()函数和一个循环来获取多个屏幕截图,并使用连续顺序中的名称保存它们,将1保存到循环编号。

问题是我需要将这些图像放入“电影”中,格式并不重要。不幸的是,我不知道如何做到这一点,我正在我的iPad上旅行。

编辑:我的意思是我需要能够通过这些屏幕截图从我的Java应用程序创建电影或动画的东西。

3 个答案:

答案 0 :(得分:4)

您可以创建没有第三方库的动画GIF,如下所示:

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));
}

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

答案 1 :(得分:1)

(过时的,废弃的)Java Media Framework可以将JPEG转换为MOV。虽然注意它们具有残酷的压缩(使用特定的压缩编解码器实现),并导致巨大的文件大小。在this answer中可以看到一个例子。

答案 2 :(得分:-1)

您可以使用任何GIF创建器从图片生成电影。 你需要在你的iPad上做这个吗? maybe use this app

或使用this

等网站

但这究竟与Java有什么关系呢?

编辑:在评论中发布,this帖子中提供了一个从图像中创建GIF的Java类