Android: Add text to a bitmap - android

Is there a way to add text to a bitmap with rotation and text customization, and them save the bitmap with text to the device storage. Also can this be applied to adding images to an image?

Just create a Canvas backed up by a Bitmap with new Canvas(myMutableBitmap) and do your text drawing in it. Then save the bitmap back with myMutableBitmap.compress().

Related

Saving Multiple Circles to Bitmap android

I need to save a number of formatted circles to a Bitmap whenever a button is pushed, and then draw those circles to the canvas. I was looking around, but had a great deal of trouble finding resources on how to save a shape I had created that was not an already created resource file. Any advice on how to go about this would be appreciated.
Say you are in your custom view, but it should work also with a OnTouchListener in any view.
Create a mutable bitmap with Bitmap.createBitmap() with the same width and height of your view
use your logic for getting information of the circles, for example you can use onTouchEvent() to get the coordinates of you finger(s)
with this information you can use Bitmap.setPixel() to save it to the bitmap
optional, onDraw() you can draw your bitmap with Canvas.drawBitmap()
In case you need to draw shapes to the bitmap you can create a new Canvas(bitmap) and draw on this canvas: the pixels will be set on the bitmap.

How to create an image by giving the specifications

I want to create an image (e.g coupons) in my applications by giving the specifications in the edit text.Let suppose i have 3 edit texts for name,color,content respectively.when i fill these details and click on submit button, A image should generate containing all the specifications that i have giving in the edit text so that user can change it any time and generate the desired image.
how can i achieve this?
You can create a Bitmap of whatever size you want for your coupons:
Bitmap coupon = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Then you create a Canvas for drawing into the Bitmap:
Canvas canvas = new Canvas(coupon);
Draw your coupon using canvas with the same calls as you would when drawing to the screen. Finally, you can display the Bitmap on the screen either in a custom view's onDraw method or by creating an ImageView and calling it's setImageBitmap method with your coupon bitmap.

How to edit image for e.g. permanently adding text on the image which becomes the part of that image in android?

I have following requirement
i have a image in my android phone.
now i want to edit this image in such a way that it becomes the part of my image
for e.g., say i want to write text on the image like date when it is edited and write a name on the image which i will give.
now save that image and now if i open that image again the text which i have written will also be shown because now it becomes the part of my image.(This something like what we do it in photoshop)
i hope my requirement is very clear.
i searched on the stackoverflow but didnt get any proper working way to do.
i only found the image with text overlayed on that not actual image edited.
thank you all who will try to solve my query.
You should open the image as a Bitmap and edit it with a canvas then just save it to the SDcard.
in = new FileInputStream("/sdcard/test2.png");
buf = new BufferedInputStream(in);
Bitmap bMap = BitmapFactory.decodeStream(buf);
Then assign it to a canvas:
Canvas myCanvas = new Canvas(bMap);
Edit! Apparently you have to make a copy of the bitmap before changing it: Look at this question for more informations: Loading a resource to a mutable bitmap
To draw text or anything else on a canvas you need a paint.
Paint myPaint = new Paint();
myPaint.setColor(Color.Black);
myPaint.setTextSize(25);
myCanvas.drawText("Your text", positionX, positionY, myPaint); //Set the position where you like
Then you save your image
try {
FileOutputStream out = new FileOutputStream("Your filename");
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
References:
http://developer.android.com/reference/android/graphics/Canvas.html
Save bitmap to location
You should add this image as background of relative layout and add a textView over this layout . Additionally you can move text over the relative layout. When you save this action you please note position, text value, text size, font etc of textView and then you need to take an invisible relative layout in background which is similar in actual image size and put a textView in proportion of earlier noted dimension so it will not loose image quality and textView action. Finally you need to take a snapshot of this invisible relative layout and convert this into jpeg or as suitable to you. It works and real use of taking screenshot programmatically. if you are willing to implement this i can provide code as well.

Android: how to simply draw a bitmap on another bitmap

I'm new to Android dev and I'm having a hard time trying to do something which seems obvious to me: drawing little images on top of a bigger image.
Let's say that I have a 500x500 image and I want to draw icons at different locations. Icons are png files that I load with:
Bitmap img =
BitmapFactory.decodeResource(getResources(),
R.drawable.idIcon1)
My "background image" is a LayerDrawable.
Then, I am totally lost... Do I have to create a canvas ? How to draw on my "background image" my icons at different positions?
int positionLeft=0;
int positionTop=0;
Bitmap newBitmap =Bitmap.createBitmap(backgroundBitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(backgroundBitmap, positionLeft, positionTop,null);
positionLeft=100;
positionTop=100;
canvas.drawBitmap(iconBitmap,positionLeft,positionTop,null);
imageView.setImageBitmap(newBitmap);
You're making simple things difficult. Just use a layout with android:background attribute, and then add ImageViews dynamically with the necessary bitmaps inside.

Write contents of custom View to large Image file on SD card

I have a class that extends View. I override the onDraw method and allow the user to draw on the screen. I am at the point where I want to save this view as an image. I Can use buildDrawingCache and getDrawingCache to create a bitmap that I can write to the SD card. However, the image is not good quality at a large size, it has jagged edges. Since I have a View and I use Paths I can transform all by drawing to a bigger size. I just don't know how to make the Canvas bigger so when I call getDrawingCache it doesn't crop all the paths I am just transformed. What is happening is I transform all my paths but when I write the Bitmap to file I am only getting the "viewport" of the actual screen size. I want something much bigger. Any help in the right direction would be greatly appreciated. I have been reading the docs and books and am at a loss.
Thanks
Jon
You can create another Canvas backed by a Bitmap that you create and draw all the paths to that. Then the Bitmap will hold the larger, higher resolution image. It would look something like this:
Bitmap largeBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas largeCanvas = new Canvas(largeBitmap);
// Draw the paths to this canvas and then use largeBitmap to save to a file.

Categories

Resources