I have try this
public Bitmap combineImages(Bitmap ScaledBitmap, Bitmap bit) {
int X = bit.getWidth();
int Y = bit.getHeight();
Scaled_X = ScaledBitmap.getWidth();
scaled_Y = ScaledBitmap.getHeight();
System.out.println("Combined Images");
System.out.println("Bit :" + X + "/t" + Y);
System.out.println("SCaled_Bitmap :" + Scaled_X + "\t" + scaled_Y);
overlaybitmap = Bitmap.createBitmap(ScaledBitmap.getWidth(),
ScaledBitmap.getHeight(), ScaledBitmap.getConfig());
Canvas canvas = new Canvas(overlaybitmap);
canvas.drawBitmap(ScaledBitmap, new Matrix(), null);
canvas.drawBitmap(bit, new Matrix(), null);
return overlaybitmap;
}
its not working
try this code sniplet and tweak as per your requirements
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;
}
make sure bmp1 is the bigger than bmp2
Related
I need to convert TextView to bitmap. TextView has transparency using the setAlpha() method. I am using following code
Bitmap b = getBitmapFromView(textView , 150);
try {
b.compress(Bitmap.CompressFormat.PNG, 95, new FileOutputStream(watermarkImagePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
public Bitmap getBitmapFromView(View view, int alpha) {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint alphaPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
alphaPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
alphaPaint.setColor(Color.TRANSPARENT);
Toast.makeText(VideoCaptureActivity.this, "alpha" + alpha, Toast.LENGTH_LONG).show();
alphaPaint.setAlpha(alpha);
canvas.drawBitmap(bitmap,0,0,alphaPaint);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(canvas);
return bitmap;
}
but the issue is that the result image has no transparency :(
After trying different techniques what worked for me was to make bitmap from view with full opacity and then set tranparency of bitmap. Hope it will help others having same issue
Bitmap b = addTranparencyToBitmap(getBitmapFromView(view), (int)( view.getAlpha() * 255));
try {
b.compress(Bitmap.CompressFormat.PNG, 95, new FileOutputStream(watermarkImagePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
public Bitmap getBitmapFromView(View view) {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
public Bitmap addTranparencyToBitmap(Bitmap originalBitmap, int alpha) {
Bitmap newBitmap = Bitmap.createBitmap(originalBitmap.getWidth(), originalBitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
Paint alphaPaint = new Paint();
alphaPaint.setAlpha(alpha);
canvas.drawBitmap(originalBitmap, 0, 0, alphaPaint);
return newBitmap;
}
In one of my application i need to do masking( overlapping 1 image to another image)
In my app i have to load 1 image(Bitmap) to imageview then have to apply some fram to that image i have used another imageview for that... this is totally working
My problem is that..
When i am going to save the bitmap...
using this pice of code
public static 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, 0, 0, null);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmOverlay.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ "test.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return bmOverlay;
}
i am getting
But i need it like
The best I found so far is this one:
public Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2.getHeight(), bmp1.getConfig());
float left =(bmp2.getWidth() - (bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight())))/(float)2.0;
float bmp1newW = bmp1.getWidth()*((float)bmp2.getHeight()/(float)bmp1.getHeight());
Bitmap bmp1new = getResizedBitmap(bmp1, bmp2.getHeight(), (int)bmp1newW);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1new, left ,0 , null);
canvas.drawBitmap(bmp2, new Matrix(), null);
return bmOverlay;
}
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
public static 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);
// determine the width of the canvas
int canvasWidth = canvas.getWidth();
int canvasHeight = canvas.getHeight();
//resize your bmp2
Bitmap resized = Bitmap.createScaledBitmap(bmp2, canvasWidth, canvasHeight, true);
// determine the centre of the canvas
int centreX = (canvasWidth - resized .getWidth()) /2;
int centreY = (canvasHeight - resized .getHeight()) /2
// This code can be used to alter the opacity of the image being overlayed.
//http://stackoverflow.com/a/12235235/1635441
//http://stackoverflow.com/a/5119093/1635441
//Paint p = new Paint();
//p.setXfermode(new PorterDuffXfermode(Mode.DST_ATOP)); //http://stackoverflow.com/a/17553502/1635441
//p.setAlpha(180);
//p.setARGB(a, r, g, b);
//canvas.drawBitmap(resized, centreX, centreY, p);
//canvas.drawBitmap(bmp2, 0, 0, null);
canvas.drawBitmap(resized, centreX, centreY, null);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmOverlay.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ "test.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
return bmOverlay;
}
I am trying to overlay 2 images on on top of other.
One image i getting from other class:
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
The other image is from drawable:
Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.topshow);
Then I get the overlay:
Bitmap overLay = (overlay(bitmap,icon));
Overlay func:
public static 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, 0, 0, null);
return bmOverlay;
}
And save the overlay as jpg:
overLay.compress(Bitmap.CompressFormat.JPEG, 80,new FileOutputStream(file_name + ".jpg"));
The problem is the image I get look not so good. On the border of the overlay image is interference and it looks like low quality.
Both images are 288x384. They both look good before the overlay. What can you suggest me?
Try this code. I hope it will help you.
Bitmap bMap = null;
Bitmap tempbMap = null;
tempbMap = Constants.wholeBitmap;
bMap = Bitmap.createScaledBitmap(tempbMap, 320, 320, true);
int BORDER_WIDTH = 0;
int BORDER_COLOR = Color.parseColor("#00000000");
Bitmap res = Bitmap.createBitmap(bMap.getWidth() + 2 * BORDER_WIDTH,
bMap.getHeight() + 2 * BORDER_WIDTH,
bMap.getConfig());
Canvas c = new Canvas(res);
Paint p = new Paint();
p.setColor(BORDER_COLOR);
c.drawRect(0, 0, res.getWidth(), res.getHeight(), p);
p = new Paint(Paint.FILTER_BITMAP_FLAG);
c.drawBitmap(bMap, BORDER_WIDTH, BORDER_WIDTH, p);
Bitmap bMapFinal = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), null, true);
Bitmap bitmapOverlay = BitmapFactory.decodeResource(getResources(), R.drawable.overlay_3_large);
int width = 293;
int height = 55;
int w = (int) (bMapFinal.getWidth()/2);
int h = (int) ((w * height) / width);
Bitmap scaled = Bitmap.createScaledBitmap(bitmapOverlay, w, h, true);
c.drawBitmap(scaled, bMapFinal.getWidth() - scaled.getWidth(), bMapFinal.getHeight() - scaled.getHeight(), p);
Bitmap bMapFinal_new = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), null, true);
ivImageMain.setImageBitmap(null);
ivImageMain.destroyDrawingCache();
Constants.finalBitmapForShare = bMapFinal_new;
ivImageMain.setImageBitmap(bMapFinal_new);
Thank you.
I'm trying to stitch images one below other and render the final image in WebView of Android.
This is my code for the same:
File f1 = new File(Environment.getExternalStorageDirectory() + "/mydownload/"+"1.jpg");
File f2 = new File(Environment.getExternalStorageDirectory() + "/mydownload/"+"2.jpg");
File f3 = new File(Environment.getExternalStorageDirectory() + "/mydownload/"+"3.jpg");
try {
joinImages(f1, f2, f3);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
private void joinImages(File first, File second , File third) throws IOException
{
System.out.println("in join images------------------------------");
Bitmap bmp1, bmp2, bmp3;
bmp1 = BitmapFactory.decodeFile(first.getPath());
bmp2 = BitmapFactory.decodeFile(second.getPath());
bmp3 = BitmapFactory.decodeFile(third.getPath());
/*if (bmp1 == null || bmp2 == null)
return bmp1;*/
int height = bmp1.getHeight()+bmp2.getHeight()+bmp3.getHeight();
System.out.println("height-========================== "+height);
System.out.println("widht================ "+bmp1.getWidth());
/* if (height < bmp2.getHeight())
height = bmp2.getHeight();*/
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(bmp2, bmp1.getHeight(), 0, null);
canvas.drawBitmap(bmp3,bmp1.getHeight()+bmp2.getHeight() , 0, null);
FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + "/mydownload/"+"final.jpg");
bmOverlay.compress(CompressFormat.JPEG, 80, out);
out.close();
//return bmOverlay;
}
But the image which is being save is containing only one image instead of three.
Are your drawBitmap calls offsetting the width instead of the height?
Assuming you want the bitmaps one on top of the other, should you be doing:
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(bmp2, 0, bmp1.getHeight(), null);
canvas.drawBitmap(bmp3,0, bmp1.getHeight()+bmp2.getHeight() , null);
instead of
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(bmp2, bmp1.getHeight(), 0, null);
canvas.drawBitmap(bmp3,bmp1.getHeight()+bmp2.getHeight() , 0, null);
I want to create a combined image with two different images by overlapping.
For this My code is
ImageView image = (ImageView) findViewById(R.id.imageView1);
Drawable drawableFore = getResources().getDrawable(R.drawable.foreg);
Drawable drawableBack = getResources().getDrawable(R.drawable.backg);
Bitmap bitmapFore = ((BitmapDrawable) drawableFore).getBitmap();
Bitmap bitmapBack = ((BitmapDrawable) drawableBack).getBitmap();
Bitmap scaledBitmapFore = Bitmap.createScaledBitmap(bitmapFore, 35, 35, true);
Bitmap scaledBitmapBack = Bitmap.createScaledBitmap(bitmapBack, 45, 45, true);
Bitmap combineImages = overlay(scaledBitmapBack, scaledBitmapFore);
image.setImageBitmap(combineImages);
overlay() method is
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
try
{
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
return null;
}
}
case 1 :overlay method returns null in this case.
case 2: But when I switch images like I use background image for setting in foreground and foreground image for setting in background then code works fine.
but I want the first case should work properly but it is not.
I am not getting why this is happening.
Please Help
I think it happens, because the 2nd bitmap is bigger in size. So try this:
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
try
{
int maxWidth = (bmp1.getWidth() > bmp2.getWidth() ? bmp1.getWidth() : bmp2.getWidth());
int maxHeight = (bmp1.getHeight() > bmp2.getHeight() ? bmp1.getHeight() : bmp2.getHeight());
Bitmap bmOverlay = Bitmap.createBitmap(maxWidth, maxHeight, bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
} catch (Exception e)
{
// TODO: handle exception
e.printStackTrace();
return null;
}
}