Is there anyway to convert user input text to an image file (.jpg, .bmp , .gif)

Login to reply to this topic.
Fri, 2006-02-17 09:32
Joined: 2005-07-10
Forum posts: 68
Hello,

I experiment something (a test project). I found I need to convert text to an image file. For example, I get text input from user. Then I have to convert those input text as an image file. There must be some way to do it. But I could not really find where to start for creating own image file. Could someone point me to a right direction? Thanks! 

Sat, 2006-02-18 20:17
Joined: 2004-05-24
Forum posts: 982
Re: Is there anyway to convert user input text to an image file
Something like you get from scanners? Or somthing else?
If you need an image with the text typed by the user there is a very simple method to achieve it: just draw the user text into a bitmap (you can find a lot of examples on how to use different graphic contents).

pirosl

Sat, 2006-02-18 23:20
Joined: 2004-11-29
Forum posts: 1233
Re: Is there anyway to convert user input text to an image file
Yes. You want to create a CFbsBitmap with the size you want. Then draw to it useing a CFbsBitGc. (you need CFbsDevice too as a "middle man").   

When you are done, save it to a file with the format of your choise using CImageEncoder

Search for the above classes on this forum and you will find plenty of examples on their usage.
Tue, 2006-02-21 10:26
Joined: 2005-07-10
Forum posts: 68
Re: Is there anyway to convert user input text to an image file
Thank u guys!

Problem solved!

But there are something I would like to add for future developer who will need same solution. They dont need to dig it from the very bottom, as I did so. Solution is given below:

Quote
_LIT( KImageFile, "c:\\test.jpeg");
_LIT( KImageFile2, "c:\\MyBmp.bmp");

void CMyClass::ConvertBitmapToFileL(CFbsBitmap*& aBitmap, const TDesC& aText)
{
//set size for bitmap
TInt aWidth = 200, aHeight = 200;

aBitmap = new(ELeave)CFbsBitmap();
CleanupStack::PushL(aBitmap);
User::LeaveIfError(aBitmap->Create(TSize(aWidth, aHeight), EColor64K));

CFbsBitmapDevice* bitDevice = CFbsBitmapDevice::NewL(aBitmap);
CFbsBitGc* bitGc;
User::LeaveIfError(bitDevice->CreateContext(bitGc));

CleanupStack::Pop(aBitmap);

//make your drawings here
//e.g.
bitGc->SetPenColor(KRgbBlack);
bitGc->UseFont( LatinBold19() ); // a font has to be set otherwise it panics!
bitGc->SetPenStyle(CGraphicsContext::ESolidPen);
bitGc->DrawText(aText, TPoint(10, 10));

RFs fs;
User::LeaveIfError( fs.Connect() );
// constructing encoder
CImageEncoder* imgEncoder = CImageEncoder::FileNewL( fs, KImageFile2, _L8("image/bmp"),
CImageEncoder::EOptionAlwaysThread );
// Following settings are for JPG file format
// TJpegImageData* imageData = new (ELeave) TJpegImageData;
// Set some format specific data
// imageData->iSampleScheme = TJpegImageData::EColor444;
// imageData->iQualityFactor = 95;

// Following settings are for BMP file format
TBmpImageData* imgData = new(ELeave) TBmpImageData;
imgData->iBitsPerPixel = 24;

CFrameImageData* iFrameImageData = CFrameImageData::NewL();

// frameData - ownership passed to iFrameImageData after AppendImageData
User::LeaveIfError(iFrameImageData->AppendImageData(imgData)); // or append imageData for jpg file

// Do the convert
TRequestStatus iRequesStatus;
imgEncoder->Convert( &iRequesStatus, *aBitmap, iFrameImageData);
User::WaitForRequest( iRequesStatus );

fs.Close();
delete iFrameImageData;
delete aBitmap;
delete imgEncoder;
delete bitGc;
delete bitDevice;
}

//call it like this:
TBuf<KMaxName> myText;
myText.Copy(_L("Problem Solved! MADSUM"));

CFbsBitmap *myBitmap;
ConvertBitmapToFileL(myBitmap, myText);
  • Login to reply to this topic.