I am trying to create a signature component where the user can write his signature on the view and the resulting bitmap being saved as an image. I am able to achieve this successfully.
But my issue is that the background color is of the image being saved is always transparent. In my onDraw method i do the following:
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.GREEN);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
and while writing to the file system i do the following:
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG,100, fOut);
fOut.flush();
fOut.close();
The view on the device while capturing the signature is as shown in the image below:
After the bitmap is written to the file system the image is as below:
Could some one kindly help me with this. I would like me image to have the same background as the canvas.
Thanks in advance.
From your question it seems like you want to save the entire view as a bitmap instead of the path object. To do this you can simply save the DrawingCache of the view as a Bitmap.
myView.buildDrawingCache()
Bitmapt b = myView.getDrawingCache()
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG,100, fOut);
fOut.flush();
fOut.close();
Related
This is my problem:
Capture an image from the camera
Write some text on it
save it into an app folder
the first point I have covered.
help me out for the remaining two points
Thanks in advance
try this
1 - code to write text to bitmap
Bitmap bitmap = ... // your bitmap here
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(10);
canvas.drawText("Some Text here", x, y, paint);
2 - code to save bitmap to storage
// Assume block needs to be inside a Try/Catch block.
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "File.jpg");
fOut = new FileOutputStream(file);
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
source : Save bitmap to location
How to write text on an image
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?
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);
}
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
Using Graphics2d you can create a PNG image as well:
public class Imagetest {
public static void main(String[] args) throws IOException {
File path = new File("image/base/path");
BufferedImage img = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.YELLOW);
g2d.drawLine(0, 0, 50, 50);
g2d.setColor(Color.BLACK);
g2d.drawLine(50, 50, 0, 100);
g2d.setColor(Color.RED);
g2d.drawLine(50, 50, 100, 0);
g2d.setColor(Color.GREEN);
g2d.drawLine(50, 50, 100, 100);
ImageIO.write(img, "PNG", new File(path, "1.png"));
}
}
I'm trying to fuond an image editor library that allow me to put a sticker on an image. I've found two libraries, one is Aviary , but I can't put my own stickers in it, then I've tried Android-image-edit but is very simple and it's not a library, it's an application.
Anyone knows some good libraries that allow me to do what I'm trying to do?
Thanks in advance.
I don't know a library, but if I understand you right, you want to add one Drawable onto an image?
You can do this yourself: (untested!)
public void putSticker(File file, Bitmap overlay) {
//Load bitmap from file:
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
bitmap = bitmap.copy(Bitmap.Config.RGB_565, true);
//Draw overlay:
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(overlay, 0, 0, paint);
//Save to file:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 40, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
fos.write(bitmapdata);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Maybe there is a better way for loading and saving. But drawing is the interesting part for your question, anyway.
See http://developer.android.com/reference/android/graphics/Canvas.html for more drawBitmap() possibilities. (Offsets, Scaling, ...)
I don't know, if anyone's still looking for this, but this library can you in every possible way, to add a sticker.
https://github.com/wuapnjie/StickerView
I am programmatically drawing to a canvas using data entered by the user. Once all of the data is entered, the user can flip through the images and they will be drawn to the canvas. The user has the option to save all of the images(could be several hundred). I use a runnable that runs on the UI thread that will draw and save each image(since you can't draw to a canvas from an AsyncTask). This works, but the problem I am having is if while the saving is going on, the user turns off the screen or minimizes the app. This causes the jpegs to just be black. I would like the saving to be something that could run in the background and still work.
Code used to draw to canvas and save image:
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap bitmap;
bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
// draw everything here
OutputStream stream = new FileOutputStream(imageName + ".jpg");
bitmap.compress(CompressFormat.JPEG, 100, stream);
stream.close();
}
So, is there a way for images to be drawn and saved to a file in the background while the canvas is not visible? Any help would be appreciated!
I think you can use this:
signLayout.setDrawingCacheEnabled(true); //signLayout is layout that contains image you want to save
// Save image from layout
String path = "sdcard0/test/test.jpg"; //file path you want to save
gestureSign.save(signLayout, path);
SAVE METHOD
//Save to path
public void save(View v, String SavePath)
{
if(mBitmap == null)
{
mBitmap = Bitmap.createBitmap (layoutSign.getWidth(), layoutSign.getHeight(), Bitmap.Config.RGB_565);;
}
Canvas canvas = new Canvas(mBitmap);
try
{
FileOutputStream mFileOutStream = new FileOutputStream(SavePath);
v.draw(canvas); //Lay toan bo layout
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, mFileOutStream);
mFileOutStream.flush();
mFileOutStream.close();
}
catch(Exception e)
{
Log.v("log_tag", e.toString());
}
}
I have 1 Bitmap image and i had converted that image into grayscale. My problem is I have to change the selected region of image with the original color. For that i am using getPixel() and setPixel() function on canvas in onDraw() method. I got the region also using onTouch method but i am unable to save the bitmap.
Please Help me
Thanks
Do the following to store a bitmap on android (e.g. as a png):
Bitmap bitmap = Bitmap.createBitmap(source, x, y, width, height...);
FileOutputStream fos = new FileOutputStream("/some/place/bla.png"));
target.compress(CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();