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 wri开发者_运维知识库te 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.
精彩评论