Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9019415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:46:19+00:00 2026-06-16T04:46:19+00:00

I am ingesting an AWT image (from PDF, through PDFBox) with the following code:

  • 0

I am ingesting an AWT image (from PDF, through PDFBox) with the following code:

private java.awt.Graphics2D graphics;
public void drawImage(java.awt.Image awtImage, java.awt.geom.AffineTransform at) {
    graphics.setComposite(getGraphicsState().getStrokeJavaComposite());
    graphics.setClip(getGraphicsState().getCurrentClippingPath());
    graphics.drawImage( awtImage, at, null );
}

and wish to capture/output the image as SVG. I have been using the Batik library which produces svg of the form

    <image x="0" y="0" transform="matrix(0.144,0,0,0.1439,251.521,271.844)" 
clip-path="url(#clipPath2)" width="1797" xlink:href="data:image/png;
base64,iVBORw0KGgoAAAANSUhEUgAABwUAAAV4CAMAAAB2DvLsAAADAFBMVEX////+/v56 
enpWVlZbW1taWlpZWVnHx8eRkZFVVVWMjIysrKxXV1dYWFhqamr5+fnMzMxeXl7c 
3NyUlJR/f3+3t7cAAACGhob29vYpKSliYmJPT083Nzf8/PyBgYENDQ3s7OwwMDD1 
    ...
    RERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERERE 
RPQP/R8CiIK+y8Q6KQAAAABJRU5ErkJggg==" height="1400"
 preserveAspectRatio="none" stroke-width="0" xmlns:xlink="http://www.w3.org/1999/xlink"/>

I have my own SVG library and want to add methods to my SVGImage class to produce something similar. Do I need to apply the AffineTransformation to the data, and if so how?

I would be grateful to be pointed to appropriate (F/OSS) methods or libraries that can do this. The data should be inline (as in the above example) and XML-compliant.

UPDATE: In the absence of any answer or comments I have had to write my own implementation. This wasn’t completely trivial and may not be robust or the best solution. I append it as an answer – please comment if it’s flawed or could be improved.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:46:20+00:00Added an answer on June 16, 2026 at 4:46 am

    [ANSWERING OWN QUESTION]

    I assume the java.awt.Image is a BufferedImage as that has the appropriate methods. In the present case this has worked (so far).

    The SVG needs the MIME type of the image (e.g. “image/png“).
    ImageIO needs a type such as “PNG“.
    mimeType2ImageTypeMap maps these.
    The Base64 codec is taken from Xerces but there are a lot around.
    The DOM implementation is XOM and the attribute construction provides the namespace for xlink

        public class SVGImage extends xom.nu.Element {
        private static final String DATA = "data";
        private static final String BASE64 = "base64";
        public static final String IMAGE_PNG = "image/png";
        public static final String PNG = "PNG";
    
        private static final String XLINK_PREF = "xlink";
        private static final String HREF = "href";
        private static final String XLINK_NS = "http://www.w3.org/1999/xlink";
    
            public void readImageData(BufferedImage bufferedImage, String mimeType) {
        String type = mimeType2ImageTypeMap.get(mimeType);
        if (type == null) {
            throw new RuntimeException("Cannot convert mimeType: "+mimeType);
        }
        double x = bufferedImage.getMinX();
        double y = bufferedImage.getMinY();
        double height = bufferedImage.getHeight();
        double width = bufferedImage.getWidth();
        this.setX(x);
        this.setY(y);
        this.setWidth(width);
        this.setHeight(height);
    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            ImageIO.write(bufferedImage, type, baos);
        } catch (IOException e) {
            throw new RuntimeException("Cannot read image", e);
        }
        byte[] byteArray = baos.toByteArray();
        String base64 = Base64.encode(byteArray);
        String attValue = DATA+":"+mimeType+";"+BASE64+","+base64;
        this.addAttribute(new Attribute(XLINK_PREF+":"+HREF, XLINK_NS, attValue));
        }
    }
    

    The code above has worked on a test case so I am optimistic it will scale to large Images and other MIME types but haven’t tested them.

    YES: I do have to apply the affine transformation. I already have utility routines for this which convert a 3*2 matrix of doubles to an SVG transform="matrix(...)" attribute. So my final code is:

    Transform2 t2 = new Transform2(at);              // converts matrix syntax
    BufferedImage bImage = (BufferedImage) awtImage;
    SVGImage svgImage = new SVGImage();
    svgImage.setTransform(t2);
    svgImage.readImageData(bImage, SVGImage.IMAGE_PNG);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: public static void PrependEntitiesToFile(string pathToFile) { char[] buffer =
Let's say that I got the following code in a page: elit ac <a
for Ingesting assets to Media Services from another blob storage account, Followed the steps
I am trying to get 2D array data from a Java properties file and
Inserting rows one by one from a 600 record data-set is taking almost an
When inserting copy into an HTML document I get from sources such as word
After using Instruments, I have found the a spot in my code that is
I wanted to convert some xml to a magazine like pdf document. A lot
I'm writing a search engine in C#, retrieving rows from a SQL database. I'd
For an event we need the following setup: Multiple computers running a kinda of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.