Results 1 to 7 of 7
  1. #1
    Cleeve is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default How do I add text after using MemoryImageSource

    I've followed all the tutorials adding text to images and buffered images with no problems until I try to add text to an image created using MemoryImageSource. I get an error message saying that the usual method doesn't work with images produced by ImageProducer. The image starts life as an integer array with ARGB format so converting it into a buffered image didn't work either as these only seem to work with text when defined as byte arrays.
    BTW I need to store the image with overlaid text on disk, so I don't think it is just writing to a JPanel or a JFrame.
    Does anyone have a sample of code showing how to add some text to an image produced by MemoryImageSource.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    I get an error message
    Please copy and paste here the full text of the error message.

    how to add some text to an image
    If you use the Graphics methods to draw the image and then draw a String over it, is that what you mean?

  3. #3
    Cleeve is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    I tried so many variations I can't reproduce the same error message. However, I assume from your response that it should be possible and it was something else I did that caused the error i.r.o. the use of ImageProducer.

    Extracting the relevant bits, my code is:

    DirectColorModel dcm = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF);
    MemoryImageSource sourceImage = new MemoryImageSource(320, 240, dcm, outpixels, 0, 320);
    ........< process(inIntegerArray,outpixels) >...

    xFrame = createImage(sourceImage); //where xFrame is defined as Image
    BufferedImage bufim = new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = bufim.createGraphics();
    g.setFont(new Font("Arial", Font.PLAIN, 12));
    g.setPaint(Color.red);
    g.drawImage(xFrame, null, null);
    g.drawString("Testing",20,100);
    imgpanel.setImage(Frame);

    The image produced in "xFrame" displays correctly but the text is never visible.
    What have I done wrong?
    (PS I've used absolute values here for brevity)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    Can you make a small program (a SSCCE) that can be used for testing?

  5. #5
    Cleeve is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    OK, I'll start with apologies for the quality of this bit of hacked code but, functionally, it does the same as the main program and exhibits the same fault.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import javax.imageio.*;
    import java.awt.Font;
    import java.awt.Dimension;
    import java.awt.image.*;
    import java.lang.*;
    import java.io.*;
    
    
    public class MIS extends JFrame {
      public ImagePanel imgpanel = null;
    
    	public static void main(String[] args)
    	{
    		MIS jmfcam = new MIS();		
    	}
    
    
    public MIS()
    {
    
        Container content = getContentPane();
        JPanel setup = new JPanel();
        imgpanel  = new ImagePanel();
        imgpanel.setPreferredSize(new Dimension(324, 244));
        content.add(setup, BorderLayout.NORTH);
        setup.add(imgpanel);
        int[] pixin = new int[320*240];		// source image frame (from camera)
        int[] outpixels=new int[320*240];		//After processing
       pack();
       setVisible(true);
    
    	for (int i=0; i<240*320;i++) {pixin[i]=i;}    //Create an integer array to act as image source
    
    	DirectColorModel dcm = new DirectColorModel(32, 0x00FF0000, 0x0000FF00, 0x000000FF);
    	MemoryImageSource sourceImage = new MemoryImageSource(320, 240, dcm, outpixels, 0, 320);
    
    		System.arraycopy(pixin, 0, outpixels, 0, outpixels.length);
    
    	Image xFrame = createImage(sourceImage); 
    	BufferedImage bufim = new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB);
    	Graphics2D g = bufim.createGraphics();
    	g.setFont(new Font("Arial", Font.PLAIN, 12));
    	g.setPaint(Color.red);
    	g.drawImage(xFrame, null, null);
    	g.drawString("Testing",20,100);
    	imgpanel.setImage(xFrame);
    }
    
    public class ImagePanel extends Panel 
      {
          public Image myimg = null;
          public ImagePanel() 
          {
            setSize(320,240);
          }
        
          public void setImage(Image img) 
          {
            this.myimg = img;
            repaint();
          }
        
          public void update(Graphics g) 
          {
            if (myimg != null) 
            {
              g.drawImage(myimg, 0, 0, this);
            }
          }
      }
    
    }

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,903
    Rep Power
    25

    Default

    Try this:

    imgpanel.setImage(bufim); //xFrame);

  7. #7
    Cleeve is offline Member
    Join Date
    Jun 2011
    Posts
    14
    Rep Power
    0

    Default

    That's great. As you already know, it works. Thank you.
    I'll figure out why it works tomorrow.

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 03:38 PM
  2. Replies: 8
    Last Post: 05-18-2011, 04:01 PM
  3. Replies: 7
    Last Post: 03-05-2011, 10:25 AM
  4. Replies: 0
    Last Post: 12-09-2010, 06:56 PM
  5. Replies: 0
    Last Post: 05-26-2008, 05:26 PM

Tags for this Thread

Posting Permissions

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