Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create image programmatically on Java, Android?

Tags:

I need to create .jpeg/.png file on my Android application programmatically. I have simple image (black background), and it need to write some text on it programmatically. How can I do it? Is it possible?

like image 706
user1023177 Avatar asked Nov 12 '11 19:11

user1023177


People also ask

How to add image in android programmatically?

To show image by XML, we need to add ImageView tag in our layout in XML. ImageView tag has the attribute android:src which will refer image kept in res/drawable directory. To show image programmatically, instantiate ImageView in Activity class and assign image by setImageResource() and add it to layout by LinearLayout.

How to add ImageView in android studio?

For adding an image from Android Studio, Drag the ImageView widget to the activity area of the application, a pop-up dialogue box will open choose from the wide range of drawable resources and click “OK“.

Which method is used to change the ImageView background programmatically?

setBackgroundResource() method is used to change the button background programmatically. setBackgroundResource(int id) accepts id of drawable resource and applies the background to the button.

How can I set image width and height in android?

If you want to just fit the image in image view you can use" wrap content" in height and width property with scale-type but if you want to set manually you have to use LayoutParams. Layoutparams is efficient for setting the layout height and width programmatically.


2 Answers

It's definately possible.

To write text on an image you have to load the image in to a Bitmap object. Then draw on that bitmap with the Canvas and Paint functions. When you're done drawing you simply output the Bitmap to a file.

If you're just using a black background, it's probably better for you to simply create a blank bitmap on a canvas, fill it black, draw text and then dump to a Bitmap.

I used this tutorial to learn the basics of the canvas and paint.

This is the code that you'll be looking for to turn the canvas in to an image file:

OutputStream os = null; 
try { 
    File file = new File(dir, "image" + System.currentTimeMillis() + ".png");
    os = new FileOutputStream(file); 
    finalBMP.compress(CompressFormat.PNG, 100, os);
    finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
    screenGrabFilePath = file.getPath();
} catch(IOException e) { 
    finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
    Log.e("combineImages", "problem combining images", e); 
}
like image 77
Rev Tyler Avatar answered Oct 27 '22 00:10

Rev Tyler


Yes, see here

Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);

You can also use awt's Graphics2D with this compatibility project

like image 33
Bozho Avatar answered Oct 27 '22 00:10

Bozho