Android Converting Ninepatch Resource To Bitmap - java

I have a 9 patch drawable as XML. When I try to use BitmapFactory.decode to get bitmap out of it, it returns null. Is there anyway to get a bitmap from this resource?
nine_patch.xml:
<?xml version="1.0" encoding="utf-8"?>
<nine-patch xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/empty_icon_video" >
</nine-patch>
and the code is:
BitmapFactory.decodeResource(mResources, resId);

Bitmap bmp = Bitmap.createBitmap(yourWidth, yourHeight, Bitmap.Config.ARGB_8888);
Drawable drawable = getResources().getDrawable(R.drawable.resId);
Canvas canvas = new Canvas(bmp);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
Try this. Now the bitmap will be having the ninepatch drawn over it. You can supply a different width and height.

You need to use a NinePatchDrawable API here -
NinePatchDrawable npDrawable = (NinePatchDrawable)getResources().getDrawable(R.drawable.empty_icon_video);

Related

How can I put the contents of Layout 1 as the background of Layout 2?

In the first layout, several text views and image views are set. How can I set the background of the second layout with the image I capture from the first layout by clicking a button?
I used this code
binding.cardLAYOUT.setDrawingCacheEnabled(true);
binding.cardLAYOUT.buildDrawingCache();
Bitmap bitmap =
Bitmap.createBitmap(binding.cardLAYOUT.getWidth(),
binding.cardLAYOUT.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Drawable bgDrawable = binding.cardLAYOUT.getBackground();
bgDrawable.draw(canvas);
binding.cardLAYOUT.draw(canvas);
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
binding.cardbacklayout.setBackground(drawable);

draw bitmap over another bitmap

i need to draw a bitmap over another bitmap like the image below in java android
this is my code
private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}
how can I determine where to draw Bitmap2 (Coordinates for the start point)
Looking at the pictures, you want it centered? Then the answer is
x= (bitmap1.getWidth()- bitmap2.getWidth)/2
y= (bitmap1.getHeight()- bitmap2.getHeight)/2
Basically, figure out the extra space in bitmap1 over bitmap 2, and cut it in half.

Draw in a canvas and save it to a larger image

I have a requirement, like drawing something in canvas and saving it to a larger image. As of now whatever I draw inside onDraw() method and save, it gives device provided image/canvas size, say something around 538(w)/852(h). I need image of almost double size, around 1000(w)/1500(h) without losing resolution. Any sample code, reference link would help definitely. Thanks in advance.
One way is to create Bitmap of desired size and draw on it using canvas:
int w = 1000, h = 1500;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
Canvas canvas = new Canvas(bmp);
Finally you will have you bmp with your drawing and necessary dimentions.
EDIT::
#Override
protected void onDraw(Canvas canvas) {
int w = 1000, h = 1500;
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
Canvas mycanvas = new Canvas(bmp);
super.onDraw(mycanvas);
...
}
First get bitmap of your view using the function getDrawingCache(), then scale the bitmap as per your requirements.
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
//bitmap = bitmap.createScaledBitmap(..as your requirements..);
EDIT
You are saying that you want image in higher quality. You can't simply get a higher quality image from a lower quality image. However you can apply bitmap filtering to get slightly better quality.
Bitmap bitmap = getDrawingCache();
Bitmap output = Bitmap.createBitmap(/*width, height, bla bla bla*/);
Canvas canvas = new Canvas(output);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
Rect srcRect = new Rect(0,0,bitmap.getWidth(), bitmap.getHeight());
Rect destRect = new Rect(0,0,output.getWidth(), output.getHeight());
canvas.drawBitmap(bitmap, srcRect, destRect, paint);

How to convert the view into bitmap without adding into the UI in Android

I am using View in android and I need to convert this to Bitmap without adding this into the activity.
view.setDrawingCacheEnabled(true);
Bitmap bitmap= view.getDrawingCache();
It returns bitmap as null.
Also I have tried the method view.buildDrawingCache() but still getDrawingCache() return null.
Thanks in advance.
First you need to create empty bitmap and get canvas from it.
use below code for this
int w = WIDTH_PX, h = HEIGHT_PX;
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
Canvas canvas = new Canvas(bmp);
Now you need to draw you view in this bitmap.
use below code for this
LinearLayout layout = new LinearLayout(getContext());
TextView textView = new TextView(getContext());
int padding = 4;
textView.setPadding(padding, padding, padding, padding);
textView.setVisibility(View.VISIBLE);
textView.setText("Hello how are you");
layout.addView(textView);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
layout.measure(canvas.getWidth(), canvas.getHeight());
layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());
layout.draw(canvas);
now your view is converted into the bitmap (bmp).

combine two images which is overlay

Hello i have a two image view one with picture which is selected from camera and another imageview is only with TEXT like "Made Hawk Nelson" the image of two imageview is below
xml code is below
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="7"
android:scaleType="fitXY" >
<ImageView
android:id="#+id/imgSelectedPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/txt_made_hawk_nelson"
android:layout_centerInParent="true" />
</RelativeLayout>
Above is half screen code and above image is also half screen
Now i want to save this picture can any body help me how can i do that? may be CANVAS will help me but i do not know how to do that so please can any body help me
You have the right idea. The canvas would be the simplest way of doing it.
But you have to understand first that those ImageViews are only on-screen representation and to create a Bitmap with those two images over-layered have little to do with the on-screen representation of it.
As in memory representations of the images you'll have Drawables (which are pre-scaled according to the screen size so their size will vary from device to device as per ldpi, mdpi, hdpi and xhdpi folders) and Bitmaps, which are absolute representations.
And all that I just said will vary as per your application and I won't be giving you the exact solution but explain you all the concepts:
Just as example, let's say you have both the background and the text as Bitmaps object, so your code would be:
// Init our overlay bitmap
Bitmap bmp = backgroundBitmap.copy(Bitmap.Config.ARGB_8888, true);
// Init the canvas
Canvas canvas = new Canvas(bmp);
// Draw the text on top of the canvas
canvas.drawBitmap(textBitmap, 0, 0, null);
// now bmp have the two overlayed:
you can (and should) do some math and use the values 0, 0 from the drawBitmap() method to center the text on the canvas.
Alternatively, if you have a drawable (e.g. getResources.getDrawable(R.drawable.bkgr); ) you can use the draw() method to draw to the canvas and use the getIntrinsicHeight and getIntrinsicWidth to create the bitmap using this method
happy coding!
Change the layout as
<Framelayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="7" >
<ImageView
android:id="#+id/imgSelectedPhoto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/txt_made_hawk_nelson"
android:layout_centerInParent="true" />
set setDrawingCacheEnabled(true); and create bitmap from it as.
Bitmap bitmap;
// frmCaptureThis is the root framelayout (this contains your imageviews)
View v1 = frmCaptureThis;
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
saveImgToSDcard(bitmap); // function to saves the image to sd card
this may Helps you , Here need to change function with Required String,and pass your Background drawable Image,
and Adjust font Size,TextColor,And Alignment Using Canvas And Create one Single Bitmap and check it.
private Bitmap getThumb(String strangle, String strnote, int width, int height) {
//Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.transperet_bg);
Bitmap bmOverlay = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmOverlay);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(20);
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
// if the background image is defined in main.xml, omit this line
canvas.drawARGB(140, 0, 0, 0);
//canvas.drawBitmap(mBitmap, 0, 0, null);
// draw the text and the point
paint.setTextAlign(Paint.Align.LEFT);
canvas.drawText("Head Angel = " + strangle, 10, 20, paint);
strnote = edtnote.getText().toString();
if (TextUtils.isEmpty(strnote)) {
strnote = "Note";
}
canvas.drawText(strnote, 10, 50, paint);
paint.setTextAlign(Paint.Align.RIGHT);
canvas.drawText("www.moovemtb.com", width-60, 50, paint);
canvas.drawText("Head Angel App", width-60, 20, paint);
canvas.drawPoint(30.0f, height/2, paint);
return bmOverlay;
}
Please try this, it works well
BufferedImage img1 = ImageIO.read(new File(pathtoImage)); //first image
BufferedImage img2 = ImageIO.read(new File(pathtoOverlay)); //overlay text image
BufferedImage combinedImage = new BufferedImage(img1.getWidth(),img1.getHeight(),BufferedImage.TYPE_INT_RGB);
Graphics g = combinedImage.getGraphics();
g.drawImage(img1, 0, 0, null);
g.drawImage(img2, 0, 0, null);
ImageIO.write(combinedImage,"JPG",new File(pathToBeSaved,"combined.jpg");

Categories