Results 1 to 7 of 7
- 07-29-2011, 07:26 PM #1
Member
- Join Date
- Jun 2011
- Posts
- 14
- Rep Power
- 0
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.
- 07-29-2011, 08:10 PM #2
Please copy and paste here the full text of the error message.I get an error message
If you use the Graphics methods to draw the image and then draw a String over it, is that what you mean?how to add some text to an image
- 08-03-2011, 04:45 PM #3
Member
- Join Date
- Jun 2011
- Posts
- 14
- Rep Power
- 0
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)
- 08-03-2011, 04:49 PM #4
Can you make a small program (a SSCCE) that can be used for testing?
- 08-04-2011, 12:39 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 14
- Rep Power
- 0
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); } } } }
- 08-04-2011, 12:52 AM #6
Try this:
imgpanel.setImage(bufim); //xFrame);
- 08-04-2011, 02:27 AM #7
Member
- Join Date
- Jun 2011
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Reading from a text file, then writing back to Text Area in Reverse
By medic642 in forum New To JavaReplies: 8Last Post: 07-17-2011, 02:38 PM -
Doubt in assigning pixel values in MemoryImageSource
By atiprashant in forum Java 2DReplies: 8Last Post: 05-18-2011, 03:01 PM -
Transforming xml to text keeps placing blank line at beginning of text file
By DerekRaimann in forum Advanced JavaReplies: 7Last Post: 03-05-2011, 09:25 AM -
Applet program to open a text file and display the content in text area
By bitse in forum Java AppletsReplies: 0Last Post: 12-09-2010, 05:56 PM -
Unsupported Content-Type: text/html Supported ones are: [text/xml]
By luislopezco in forum Advanced JavaReplies: 0Last Post: 05-26-2008, 04:26 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks