Create an image from a TextView (without losing quality) - android

I have an app that will create a png image of anything the user write in the text field (custom font and color)
What I'm doing is something like this:
Set Typeface, Text Color and Text to a TextView (BG is transparent)
Using the code below, generate the image for the TextView
Bitmap returnedBitmap = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(), Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =textView.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
// draw the view on the canvas
view.draw(canvas);
FileOutputStream out = null;
out = new FileOutputStream(new File(filename));
returnedBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
The problem is that, the quality of the generated image is not the same as what is displayed in the TextView
Is there any way to improve the quality?
Is it because of the TextView not having a background? If so, is there a workaround for this? Need the generated image to only be the text, hence the transparent background
Thanks

I believe the quality depends on the screen size from you create the image. I suggest you to put the Textview in a FrameLayout and create the bitmap from the FrameLayout..
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
v.draw(c);
return b;
}

Related

Losing quality when converting View to Bitmap in Android Kotlin using android-bitmap

I'm working on Editor Application, I need to save FrameLayout in Internal Storage,
Here I have done convert Layout to bitmap code, It's Working Fine but it gives low quality image.
Here is my code and output...
private Bitmap viewToImage(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
Any suggestions to produce HD image will be appreciated
This i get low quality image
This is original image that i want in output
Thanks in advance!

Drawable loses color filter after converting into bitmap

I am trying to add a color filer in a drawable and then convert it in Bitmap. The problem is when convert the drawable into bitmap it loses it's color filter.I used drawable in imageview and its have the color filter but using bitmap in imageview doesn't have any color effect. Why this happen ? Thanks in advance.
Use a canvas to blit the Drawable back onto a bitmap:
Canvas canvas;
Drawable drawable = <yourDrawable created from wherever>;
Bitmap bmp = <your Bitmap which is the same width/height as the drawable>
// blit the drawable onto the bitmap using a Canvas
canvas = new Canvas(bmp);
drawable.draw(canvas);
http://developer.android.com/guide/topics/graphics/2d-graphics.html
I had the same problem and I figured it out finally!
We need to do following thing to get the bitmap with color filter applied on it.
image_view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(image_view.getDrawingCache());
I've faced the same issue and finally found a solution.
Drawable can have multiple states thus you might be drawing wrong state.
You should switch it to proper mode before drawing:
Drawable drawable = icon.loadDrawable(getContext());
if (drawable == null) {
return;
}
drawable.setState(new int[] {android.R.attr.state_enabled});
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);

Android Image Masking with Rotate & Scale

How to mask image such as we can scale/zoom/rotate the background image but not the mask?
I should be able to zoom the image in this mask but it is scaling whole image.
Suggest me a way to achieve this, I'm creating a photo collage app.
Blue color is background of Layout.
The white color is for mask
I'm able to achieve this type of layout with masking, but when I apply MultiTouchListener to scale and zoom, it will scale the whole image with mask and not the image inside it.
private void getMaskedBitmap() {
Bitmap bgBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.background_drawable);
ImageView bg = (ImageView) findViewById(R.id.bg);
bg.setImageBitmap(bgBitmap);
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap emptyBitmap = Bitmap.createBitmap(bgBitmap.getWidth(), bgBitmap.getHeight(), conf);
Canvas canvasBmp = new Canvas(bgBitmap);
ImageView mImageView = (ImageView) findViewById(R.id.troll_face);
Bitmap original = BitmapFactory.decodeResource(getResources(), R.drawable.random_drawable);
Bitmap mask = BitmapFactory.decodeResource(getResources(), R.drawable.mask_drawable);
original = Bitmap.createScaledBitmap(original, mask.getWidth(), mask.getHeight(), true);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(original, 0, 0, null);
mCanvas.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
mImageView.setImageBitmap(result);
//mImageView.setScaleType(ScaleType.FIT_XY);
mImageView.setBackgroundResource(R.drawable.background_drawable);
bg.setOnTouchListener(new MultiTouchListener());
mImageView.invalidate();
}
Exception on line
Canvas canvasBmp = new Canvas(bgBitmap);
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
I cannot provide you code but here is what you should do:
Get background image as bitmap and draw it on view's canvas.
Create an new (empty) bitmap of same size (use Bitmap.Config.ARGB_8888) and get its canvas, here you will draw mask.
Canvas canvasBmp = new Canvas(bmp);
Now draw bmp on view's canvas. So, that you can get mask effect on image.
Zoom (re-size etc whatever you want) background bitmap image.
Invalidate your view, and follow all steps again

Draw background on a bitmap where i'm going to draw a view that i'm going to export to a file

I'm exporting my view into a file.
My problem is that I'm using Holo Light theme but the file exported has a dark background.
The code:
Bitmap b = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(b);
miVista.draw(mCanvas);
FileOutputStream fos = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
I've tried a lot of things to paint it in another color than black like drawcolor, setpixels, etc, but I can't find the correct answer to my problem.
Link to the picture with what you can see in the terminal.
Link to the exported picture
Clear the canvas for example with
mCanvas.drawColor(Color.WHITE);
then it will have white background. What kind of background do you expect?
I've found the solution in this other post:
Convert view to bitmaps...
Bitmap b = Bitmap.createBitmap(totalWidth, totalHeight, Bitmap.Config.ARGB_8888);
Canvas mCanvas = new Canvas(b);
Drawable bgDrawable =miVista.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(mCanvas);
else
mCanvas.drawColor(Color.WHITE);
miVista.draw(mCanvas);
The solution it's to use a Drawable object...

convert view into bitmap [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting a view to Bitmap without displaying it in Android?
I am trying to convert the view into bitmap from following reference link
link text
Now the problem is how can i get the bitmap that is being converted from view only. in the example author has used relativelayout.dispatchDraw(c) but this line is giving me compile time error i.e.
The method dispatchDraw(Canvas) from
the type ViewGroup is not visible
Here is my code and i have written the following code inside onCreate function
Canvas c=null;
//Create Layout
RelativeLayout relativeView ;
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT
);
relativeView = new RelativeLayout(this);
relativeView.setLayoutParams(lp);
//Background of Layout
Bitmap viewBgrnd = BitmapFactory.decodeResource(getResources(),R.drawable.bgblack);
relativeView.setBackgroundDrawable(new BitmapDrawable(viewBgrnd));
//associated with canvas
Bitmap returnedBitmap = Bitmap.createBitmap(320,480,Bitmap.Config.ARGB_8888);
c = new Canvas(returnedBitmap);
Paint paint = new Paint();
//Create Imageview that holds image
ImageView newImage = new ImageView(this);
Bitmap srcBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.bgpink);
newImage.setImageBitmap(srcBitmap);
TextView newText = new TextView(this);
newText.setText("This is the text that its going to appear");
c.drawBitmap(viewBgrnd, 0, 0, paint);
relativeView.layout(100, 0, 256, 256);
relativeView.addView(newImage);
relativeView.addView(newText);
// here i am getting compile time error so for timing i have replaced this line
// with relativeView.draw(c);
relativeView.dispatchDraw(c);
Here the returnedBitmap should contain the image of (ImageView and TextView) but this bitmap contains only bacground bitmap of relativeView i.e. bgblack
here is my solution:
public static Bitmap getBitmapFromView(View view) {
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
bgDrawable.draw(canvas);
else
canvas.drawColor(Color.WHITE);
view.draw(canvas);
return returnedBitmap;
}
Enjoy :)
This works for me:
Bitmap viewCapture = null;
theViewYouWantToCapture.setDrawingCacheEnabled(true);
viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());
theViewYouWantToCapture.setDrawingCacheEnabled(false);
I believe the view has to be visible (ie. getVisiblity() == View.VISIBLE). If you are trying to capture it but hide it to the user at the same time, you could move it off screen or put something over it.

Categories

Resources