Android studio (Chipmunk) screen capture image gets darken - android

I am programming a simple drawing application with drawLines:
public void onDraw(Canvas canvas) {
setBackgroundColor(cfgBackground); //background
//canvas.drawColor(ConfigActivity.getCfgBackground(ConfigActivity.cfgBackground)); //to keep background when export to PNG/JPG
//title
paint.setColor(0xbb020a0d);
paint.setTextSize(40);
canvas.drawText(Title, 0, 40, paint);
paint.setColor(color);
paint.setStrokeWidth(thickness);
canvas.drawLines(lines, paint);
}
This is no problem. When export the screen to image file, the image shows normal in the preview screen (with all other photos in the phone), but when open this image, it shows darken, as following:
I have tried Method A/B/C like following, but the result not much different:
File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(sdCard, imageName);
FileOutputStream fos = null;
try {
// Make sure the Pictures directory exists.
sdCard.mkdirs();
fos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
Toast.makeText(getContext(), file + " open failed: " + e, Toast.LENGTH_SHORT).show();
e.printStackTrace();
return;
}
/* //method A: will get dark image
setDrawingCacheEnabled(true);
Bitmap b = getDrawingCache();
//b.setHasAlpha(true); //to set transparency
//Canvas canvas = new Canvas(b);
//canvas.drawColor(cfgBackground)); //trying to solve darken image
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
setDrawingCacheEnabled(false); //for next screen update
*/
Bitmap b = screenShot(this); //Method B
b.compress(Bitmap.CompressFormat.PNG, 100, fos);
try {
fos.flush();
fos.close();
} catch (IOException e) {
Toast.makeText(getContext(), file + " save failed: " + e, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Toast.makeText(getContext(), file + " saved in Albums", Toast.LENGTH_SHORT).show();
//add to gallary
getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.toString())));
Two functions:
//Method C
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(cfgBackground);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
//Method B
public Bitmap screenShot(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),
view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
Anyone encountered this problem?

I finally found the solution (although not sure why): the background color should be 0xFFrrggbb, eg. 0xffeeee00, though the background looks ugly but just needs more finetune...
If you know why, kindly share here, appreciated!

Related

Save canvas to jpeg while canvas is not visible

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());
}
}

not able to extract the drawing cache with overlayed bitmap images

I am trying to save a canvas on an sdcard. Basically I am drawing two bitmaps (one on top of another) in the onDraw(Canvas canvas) method. But when I save the file, only the bottom layered Bitmap is stored. I am posting the code for the onDraw method here:
Paint paint = new Paint();
paint.setColor(Color.BLACK);
//rectangle for the first image
rct = new Rect(10, 10, canvas.getWidth(), canvas.getHeight());
// rectangle for the second image, the secong image is drawn where the user touches the screen
new_image = new RectF(touchX, touchY, touchX + secondBitmap.getWidth(),
touchY + secondBitmap.getHeight());
//this is the bitmap that is drawn first
canvas.drawBitmap(firstBitmap, null, rct, paint);
//this is the bitmap drawn on top of the first bitmap on user touch
canvas.drawBitmap(secondBitmap, null, new_image, paint);
canvas.save();
the code for saving the canvas on the SDcard written on the MainActivity is:
Bitmap bm = canvas.getDrawingCache() // canvas in an object of the class I extended from View
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath();
boolean exists = (new File(path)).exists();
OutputStream outStream = null;
File file = new File(path, "drawn_image" + ".jpg");
try {
outStream = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
The problem is that only the base image(firstBitmap of the onDraw() method) is saved on the SDCard instead of the entire canvas(with both the images). I am new to canvas...So any help would be very appreciated
when getDrawingcache() is called it invalidates the view to get the full cache. so debug your code and check if it is going through each and every line of your onDraw() method in view.

Android Paint PorterDuff.Mode.CLEAR

I'm working on application which is drawing on Canvas similar to Finger Paint demo from Android SDK. My problem is when I'm using PorterDuff.Mode.CLEAR. When drawing and Canvas and if I try to erase something, it's working fine. But if I try to save my image as PNG file the strokes of eraser are coloured black, and I'm not sure why is this happening. Here is an example what I'm doing :
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
Eraser :
case ERASE_MENU_ID:
mPaint.setXfermode(new PorterDuffXfermode(
PorterDuff.Mode.CLEAR));
return true;
And how I'm saving the image :
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMMddHmmss");
String dateNow = formatter.format(currentDate.getTime());
File dir = new File(mImagePath);
if(!dir.exists())
dir.mkdirs();
File file = new File(mImagePath + "/" + dateNow +".png");
FileOutputStream fos;
try {
fos = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
Toast.makeText(getApplicationContext(), "File saved at \n"+mImagePath + "/" + dateNow +".png", Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
Log.e("Panel", "FileNotFoundException", e);
}
catch (IOException e) {
Log.e("Panel", "IOEception", e);
}
return true;
And here is an example of images :
here is what my canvas looks like before saving :
and here is the image after saving it on sd card :
The problem with the fingerpaint code is that what you see is not the same that is compressed into the png. Look at onDraw(). First you draw the screen white. Then you add the Bitmap. Because you used Porter Duff Clear the erased part of the bitmap contains actually transparent black pixels (value 0x00000000). But because you have the white background these black pixels show as white.
To fix this either change your save code to do the same thing as the draw code
try {
fos = new FileOutputStream(file);
Bitmap saveBitmap = Bitmap.createBitmap(mBitmap);
Canvas c = new Canvas(saveBitmap);
c.drawColor(0xFFFFFFFF);
c.drawBitmap(mBitmap,0,0,null);
saveBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
saveBitmap.recycle();
...
or don't use PortDuff.Clear:
case ERASE_MENU_ID:
mPaint.setColor(Color.WHITE);

How to paint on Image and save that image in to Android?

I am new to canvas. I want to use the My already saved Image and want some paint on that image. after that i want to save it.
I know that with using Canvas it is possible. I can able to do painting on the Image but while i am going to store that image it only saved the painting. Not the Image with painting.
So can anybudy tell me code of how to paint on image and save that image ?
Thanks.
Here is my Code that use to do paint on the SurfaceView.
Source Code:
#Override
public void run() {
//Canvas canvas = null;
while (_run){
try{
canvas = mSurfaceHolder.lockCanvas(null);
if(mBitmap == null){
mBitmap = Bitmap.createBitmap (1, 1, Bitmap.Config.ARGB_8888);
}
final Canvas c = new Canvas (mBitmap);
//canvas.drawColor(0, PorterDuff.Mode.CLEAR);
c.drawColor(0, PorterDuff.Mode.CLEAR);
canvas.drawColor(Color.WHITE);
// Bitmap kangoo = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
// if(!(DrawingActivity.imagePath==null)){
// canvas.drawBitmap(DrawingActivity.mBitmap, 0, 0, null);
// }
commandManager.executeAll(c);
canvas.drawBitmap (mBitmap, 0, 0,null);
} finally {
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
}
I am using mBitmap to save the Bitmap to the SDCard.
Your problem is your drawing over and over on your entire canvas:
final Canvas c = new Canvas (mBitmap); // creates a new canvas with your image is painted background
c.drawColor(0, PorterDuff.Mode.CLEAR); // this makes your whole Canvas transparent
canvas.drawColor(Color.WHITE); // this makes it all white on another canvas
canvas.drawBitmap (mBitmap, 0, 0,null); // this draws your bitmap on another canvas
Use logic roughly like this:
#Override
public void run() {
Canvas c = new Canvas(mBitmap);
/* Paint your things here, example: c.drawLine()... Beware c.drawColor will fill your canvas, so your bitmap will be cleared!!!*/
...
/* Now mBitmap will have both the original image & your painting */
String path = Environment.getExternalStorageDirectory().toString(); // this is the sd card
OutputStream fOut = null;
File file = new File(path, "MyImage.jpg");
fOut = new FileOutputStream(file);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
}
Also don't forget to add necessary permission to save your file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
outside <application></application> in your manifest file.

How to Save the drawing canvas in android?

I am using this API demo of the Developer site, THIS DEMO.
But i am wonder that how to save that image in to My Andrtoid Device.
Is please anyone give the Code to save that drawn image to the Android Device.
Thanks.
try this code
View content = your_view;
content.setDrawingCacheEnabled(true);
content.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = content.getDrawingCache();
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(path+"/image.png");
FileOutputStream ostream;
try {
file.createNewFile();
ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.flush();
ostream.close();
Toast.makeText(getApplicationContext(), "image saved", 5000).show();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "error", 5000).show();
}
drawView.setDrawingCacheEnabled(true);
Bitmap bm = null;
drawView.destroyDrawingCache();
bm=drawView.getDrawingCache();
Then write the bitmap to file using bitmap factory.
One option is create another Canvas (as shown below) and repeat all your drawing on this new canvas.
Once done, call drawBitmap.
Bitmap bitmap = new Bitmap(// Set the params you like //);
Canvas canvas = new Canvas(bitmap);
// Do all your drawings here
canvas.drawBitmap(// The first picture //);
The best would be if there was a way to copy an existing canvas and then you wont need to re-draw everything but I couldn't find one.
I have implemented the below approach & worked for me.
Get your CustomView by using its id from xml file but not by instantiating the Customview.
View v = findViewById(R.id.custom_view);
//don't get customview by this way, View v = new CustomView(this);
int canvasWidth = v.getWidth();
int canvasHeight = v.getHeight();
Bitmap bitmap = Bitmap.createBitmap(canvasWidth, canvasHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
ImageView imageView = findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
All code should be inside saveButton click listener.

Categories

Resources