Android: print date and time on photo captured by my application - java

In my android application I use camera to capture photo. I want to print date and time on captured photo. As in normal camera there is an option that, if you set date and time on camera then it will printed on right lower side of the photo.
To capture the photo I use cameraIntent:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
and in onActivityResult I save in photo.
Now how I print date and time on this photo.

This code adding a image on top of another image u can use this...
Bitmap bottomImage = BitmapFactory.decodeResource(ctx.getResources(),
R.drawable.first);
Bitmap topImage = BitmapFactory.decodeResource(ctx.getResources(),
R.drawable.second);
Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
float f=(float) 0.1;
comboImage.drawBitmap(topImage, 38f, 35f, null);

open the bitmap as a canvas and write the date/time as text wherever you like. The changes would would saved on the bitmap.
Some sample code is as follows. Google Paint and Canvas classes for more options
Canvas canvas = new Canvas(bmp); //bmp is the bitmap to dwaw into
Paint paint== new Paint();
paint.setColor(Color.YELLOW);
paint.setTextSize(28);
paint.setTextAlign(Paint.Align.CENTER);
Finally, we can draw text with this font via the following Canvas method:
canvas.drawText("This is a test!", 100, 100, paint);
The first parameter is the text to draw. The next two parameters are the coordinates
where the text should be drawn to ( play with them to get the position right). Then is Paint instance. No need to use drawBimtap. Whatever you will do on the canvas will be saved on the original bitmap directly without over-writing.

Related

How to set an Image's alpha in android

so I'm in a bit of a pickle. I know how to set the alpha value of a bitmap in android. What I don't know how to do is make is reversible. So, let's say someone wanted to set the alpha of an image to 50%, so they do. Now lets say they wanted to set it 75% (keep in mind, this is of the original image alpha value). Currently, what I have is a function that will set the alpha value of the current image, so it would be 75% of the 50% alpha value if that makes sense. How can I make it so that it accounts for the original image?
public Pixmap setAlpha(float newAlpha) { //integer between 0-100
if (newAlpha != alpha) { //to check if the current alpha value of the image is equal to your desired alpha. to avoid always halving you alpha value
float test = newAlpha/100.0f;
float test2 = test * 255;
alpha = test2;
Bitmap newBM = Bitmap.createBitmap(backupImg.getBitmap().getWidth(),backupImg.getBitmap().getHeight(), Bitmap.Config.ARGB_8888);
Canvas cc = new Canvas(newBM);
cc.drawARGB(0,0,0,0);
Paint newPaint = new Paint();
newPaint.setAlpha((int)test2);
cc.drawBitmap(backupImg.getBitmap(), 0, 0, newPaint);
img.setBitmap(newBM);
return img;
} else {
return img;
}
}
The Pixmap part is just a custom Bitmap class. backupImg is just a copy of img, created in the constructor of the object this function belongs to.
please keep in mind that this will be a canvas based bitmap. If I recall correctly imageView's aren't drawn on the canvas? So, as a further example. Imagine a sprite drawn to the canvas that you want to alter the alpha of. So you do it using the function I've posted. Now, let's say you want to undo the changes and restore it to the sprite's original alpha, of some other value. Well, you can't because the alpha value of the image has been changed permanently. What I want to do is store reference to the original image with another variable, and refer to that whenever I need to adjust the alpha value of the image. Hopefully that makes sense
Why don't you set alpha to the ImageView instead of setting it to a bitmap.
By setting alpha to the ImageView or say any view you can reverse it easily.
Refer to this answer in order to do it.
You can do it from xml. Just add the below line in the imageview tag:-
android:alpha="0.5"
You can set alpha between 0 to 1. Above line will set alpha to half that is 0.5.
For bitmap:
Bitmap newBitmap = Bitmap.createBitmap(originalBitmap.getWidth(),
originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
Paint alphaPaint = new Paint();
alphaPaint.setAlpha(75);
canvas.drawBitmap(originalBitmap, 0, 0, alphaPaint);
I've solved the problem myself. So, there is nothing wrong with the function I wrote. The problem instead lies within how Java uses pointers. because everything is passed via reference, I was actually referencing the same object, rather than creating two separate objects. So instead of:
Bitmap oldBM = new Bitmap();
Bitmap newBM = oldBM;
you would instead want to do
Bitmap oldBM = new Bitmap();
Bitmap newBM = new Bitmap(using old bitmap's value);

ImageView spread out animation

I basically have an ImageView which got modified with Canvas and looks like a cirlce. (original image had the dimensions of a square (500x500))
Image what the starting position looks like:
http://imgur.com/bvXdLoP
(red is transparent and got removed with help of the Canvas method)
The animation should take aroud 1000 miliseconds and during this time step for step restore to the original picture. So in the end there should be a sqaure again.
In other words the cut off corners, which are the differnce between a square and a circle (and red marked in the image), get step for step restored in around 1000 milliseconds, with a sort of spreading looking animation.
Don't really have any clue on how to achieve this, so I can just share the Canvas method I used to cut off the corners (if it helps :X):
private Bitmap createCircleImage(Bitmap bitmap) {
Bitmap bmp;
bmp = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(bitmap,
BitmapShader.TileMode.CLAMP,
BitmapShader.TileMode.CLAMP);
float radius = bitmap.getWidth() / 2f;
Canvas canvas = new Canvas(bmp);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
canvas.drawCircle(bitmap.getWidth()/2,bitmap.getHeight()/2,bitmap.getHeight()/2, paint);
return bmp;
}
Appreciate any help,
thank you!
There are many ways to achieve such behavior. For example you can create custom view and override its onDraw method such as
#Override
public void onDraw(Canvas canvas) {
canvas.drawCircle(viewWidth/2, viewHeight/2, currentRadius, paint);
}
and then you can just increase the radius little by little and invalidate the view.Since view is by default will clip on its clipBounds (you can only draw inside viewidth x viewheight rectangle unless you set it otherwise) you will get the effect that you want. And then you can tweak the interpolation to achieve smoother and more natural animation.
note: a bitmap shader is attached to the paint (you already know chow to create it). I don't include it in the code, since you shouldn't initialize it inside onDraw method for performance reason.

Android Bitmap: Convert transparent pixels to a color

I have an Android app that loads an image as a bitmap and displays it in an ImageView. The problem is that the image appears to have a transparent background; this causes some of the black text on the image to disappear against the black background.
If I set the ImageView background to white, that sort of works, but I get ugly big borders on the image where it is stretched to fit the parent (the actual image is scaled in the middle).
So - I want to convert the transparent pixels in the Bitmap to a solid colour - but I cannot figure out how to do it!
Any help would be appreciate!
Thanks
Chris
If you are including the image as a resource, it is easiest to just edit the image yourself in a program like gimp. You can add your background there, and be sure of what it is going to look like and don't have use to processing power modifying the image each time it is loaded.
If you do not have control over the image yourself, you can modify it by doing something like, assuming your Bitmap is called image.
Bitmap imageWithBG = Bitmap.createBitmap(image.getWidth(), image.getHeight(),image.getConfig()); // Create another image the same size
imageWithBG.eraseColor(Color.WHITE); // set its background to white, or whatever color you want
Canvas canvas = new Canvas(imageWithBG); // create a canvas to draw on the new image
canvas.drawBitmap(image, 0f, 0f, null); // draw old image on the background
image.recycle(); // clear out old image
You can loop through each pixel and check if it is transparent.
Something like this. (Untested)
Bitmap b = ...;
for(int x = 0; x<b.getWidth(); x++){
for(int y = 0; y<b.getHeight(); y++){
if(b.getPixel(x, y) == Color.TRANSPARENT){
b.setPixel(x, y, Color.WHITE);
}
}
}

Overlay camera with resizeable/re-positionable item than upon click merge the images together

I need to let a user move an image around on the screen and resize it too.
That part I have accomplished already.
The part that I can't figure out is how to, upon taking a picture, freeze the moveable/resizable Image on the photo-image than merge the two together to make 1 Bitmap.
I don't really have any "snippets" of code to post. I am just hoping some people may have had an experience with this before and might care to shed some light.
EDIT. I understand that when I take the picture the image is already frozen on the screen. At that instance I capture the items location on canvas. What I need to do is to take the canvas that I already have something drawn on and somehow overlay that onto another canvas that I assigned the photo too.
EDIT #2
OK ladies and gentlemen. I figured it out. Here is the code for making an overlay for the camera (as described by mmeyer.)
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
Context ct = new Context;
RelativeLayout relativeview = new RelativeLayout(ct);
if (imageData != null) {
Context ct = HagsCamera.this;
new Intent(ct, PreviewandSend.class);
Bitmap photo = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
int picwidth= (photo.getWidth()/2);
int picheight = (photo.getHeight()/2);
Bitmap photocopy = Bitmap.createScaledBitmap(photo, picwidth, picheight, false);
Canvas c2 = crv.canvastoreturn;
c2.setBitmap(photocopy);
//photo.recycle();
ImageView newImage = new ImageView(ct);
relativeview.addView(newImage);
relativeview.draw(c2);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
photo.getWidth()/2, photo.getHeight()/2);
relativeview.setLayoutParams(lp);
Good luck to you all. I hope this helps save alot of headaches.
In short: Make a Bitmap of your picture.
Make a canvas for your overlay and toss your stuff up on there
Get a copy of your overlay's canvas
.setBitmap() that canvas and set it to your photo-bitmap.
Make a RelativeLayout and add a new ImageView to that.
Draw your canvas on the ImageView.
then figure out your layout parameters for your RelativeLayout.
Now that I think about it, maybe you can just set the top layer canvas to your Photo's Bitmap...and just save that...... You can test that yourself.
I done something in the past where I have an overlay view on top of a surface view which holds camera preview.
When the user wants to capture the pic, I take the next preview frame from Camera.PreviewCallback.onPreviewFrame() and then convert that from YUV to bitmap.
I then get a canvas against that bitmap and pass that canvas to the Draw() method of the custom view class that is my overlay view, and it will draw whatever it had been drawing onscreen to my new canvas! I can then take the bitmap and save it a jpg/png to SD.
Note that within that custom view class it has to deal with scaling due to the fact that the canvas it gets when being asked to draw on screen is a different size than the canvas it is given when asked to draw onto the canvas from the preview frame.
Beware of preview sizes and out of memory errors here though as many phones have very large default preview frame sizes and making bitmaps to draw onto with canvas can chew up memory fast. Learn about how to use Bitmap.recycle() and how to monitor native heap to combat these.
Hope that helps.
After reading the follow up comments and the issue youre having with black/blank images I decided to post some psuedo code to show how I'm doing this...
// decode the camera data into an immutable bitmap
Bitmap raw = CameraHelper.decodeYUV(frameInfo.frameData, frameInfo.frameSize.x, frameInfo.frameSize.y);
frameInfo.frameData = null; // allow large bytearry to to get gc'd
Bitmap cameraPic = raw.copy(Bitmap.Config.RGB_565, true); // make a mutable copy
Canvas c2 = (new Canvas(cameraPic)); // create a canvas from the camera pic
raw.recycle(); // we're done with raw bitmap now and can reclaim from native heap.
reticleOverlay.draw(c2); // reticle overlay is a class that implements View
String filename = acquireNextShotFilename()); // get a filename to write to SD
FileOutputStream fileoutputStream;
try {
fileoutputStream = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
MyLogger.e("Couldnt open fileoutputstream: ", e);
throw e;
}
cameraPic.compress(CompressFormat.PNG, 100, fileoutputStream);
cameraPic.recycle();
try {
fileoutputStream.flush();
fileoutputStream.close();
} catch (IOException e) {
MyLogger.e("Error writing to file: ", e);
fileoutputStream.close();
}

Tint Bitmap with Paint?

I'm trying to create a function that tints a Bitmap,
this works...
imgPaint = new Paint();
imgPaint.setColorFilter(new LightingColorFilter(color,0));
//when image is being drawn
canvas.drawBitmap(img,matrix,imgPaint);
However, when the bitmap has to be drawn constantly (every frame) , I start to see screen lag, because this didn't occur before the color filter was set, I believe that it is applying the filter every time I need the canvas drawn.
Is there a way to apply the paint once to the bitmap and have it permanently changed?
Any help appreciated :)
Create a second bitmap and draw the first bitmap into it using the color filter. Then use the second bitmap for the high-volume rendering.
EDIT: Per request, here is code that would do this:
public Bitmap makeTintedBitmap(Bitmap src, int color) {
Bitmap result = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
Canvas c = new Canvas(result);
Paint paint = new Paint();
paint.setColorFilter(new LightingColorFilter(color,0));
c.drawBitmap(src, 0, 0, paint);
return result;
}
You would then call this method once to convert a bitmap to a tinted bitmap and save the result in an instance variable. You would then use the tinted bitmap directly (without a color filter) in your method that draws to canvas. (It would also be a good idea to pre-allocate the Paint object you will be using in the main draw method and save it in an instance variable as well, rather than allocating a new Paint on every draw.)

Categories

Resources