×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    1

    replace image in SVG

    Hi,
    I would like to replace an embedded image in the SVG file.
    I tried this:

    image.setTraitNS("http://www.w3.org/1999/xlink", "href", "data:image/jpeg;base64,/9j/4AAQSkZJRgA......");


    This works only on Sun's emulator, but not on Nokia S60 FP1 emulator (or N95 mobilphone).
    The original image disappears, and nothing happens.
    Please help! Thanks

    Istvan

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    1

    Re: replace image in SVG

    Hi,
    I've met the same problem.

    You have to set image loader class while creating SVGImage:

    Code:
    svgImage1 = (SVGImage) SVGImage.createImage(getClass().
                            getResourceAsStream("/hello/newImage.svg"), new SvgImageLoader());
    In requestResource() method you can decode Base64 string:

    Code:
    package hello;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import javax.microedition.m2g.ExternalResourceHandler;
    import javax.microedition.m2g.ScalableImage;
    
    
    public class SvgImageLoader implements ExternalResourceHandler {
    
        public void requestResource(final ScalableImage scalableImage,
                final String URI) {
    
            final InputStream inputStream;
    
            System.out.println("request Resource " + URI);
            try {
                if (URI.startsWith("data:image")) {
    
                    int startPos = URI.indexOf("base64,") + 7;
                    String encodedFile = URI.substring(startPos);
    
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    
                    Base64.decode(encodedFile, bos);
                    inputStream = new ByteArrayInputStream(bos.toByteArray());
                    scalableImage.requestCompleted(URI, inputStream);
                    return;
    
                } else {
                    inputStream = getClass().getResourceAsStream(URI);
    
                    if (inputStream == null) {
                        System.out.println("Failed to load " + URI + "!");
                    }
    
                    scalableImage.requestCompleted(URI, inputStream);
                }
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
    }

Similar Threads

  1. Replies: 2
    Last Post: 2008-09-25, 06:53
  2. Opening a JPEG Image
    By ummarbhutta in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2007-02-15, 06:34
  3. how to cut some part of Image
    By mshouab in forum Mobile Java Media (Graphics & Sounds)
    Replies: 2
    Last Post: 2006-08-04, 09:05
  4. Nokia Image Converter
    By davidpurdie in forum General Development Questions
    Replies: 0
    Last Post: 2004-02-18, 15:31
  5. TinyLine SVG Minute Toolkit: Optimizing SVG for J2ME
    By girow in forum Mobile Java Media (Graphics & Sounds)
    Replies: 0
    Last Post: 2003-05-05, 18:44

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×