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.
Related
I'm trying to add a text over a barcode.
In the final stage I want to make the barcode height smaller, and put the text below the barcode, but I'm stuck to add the text.
I tried to play with the X, Y beginning, but no luck.
What am I missing?
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(finalLastID, BarcodeFormat.CODE_128, imageView.getWidth(), imageView.getHeight());
Bitmap bitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.RGB_565);
for (int i = 0; i < imageView.getWidth(); i++){
for (int j = 0; j < imageView.getHeight()-scriptTextHeight; j++){
bitmap.setPixel(i,j,bitMatrix.get(i,j)? Color.BLACK:Color.WHITE);
}
}
drawStringonBitmap(bitmap,"TEST", 0, 0, Color.RED, 60, 20, false, imageView.getWidth(), imageView.getHeight());
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
public static Bitmap drawStringonBitmap(Bitmap src, String string, int beginX, int beginY, int color, int alpha, int size, boolean underline, int width , int height) {
Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setColor(color);
paint.setAlpha(alpha);
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawBitmap(src, 0, 0, null);
canvas.drawText(string, beginX, beginY, paint);
return result;
}
Seems like a flaw in your code flow, you are using your drawStringOnBitmap() method like a void method, because you are doing nothing with the returned Bitmap, so I guess your code should be sth like :
bitmap = drawStringonBitmap(bitmap,"TEST", 0, 0, Color.RED, 60, 20, false, imageView.getWidth(), imageView.getHeight());
at that point.
Can't test it but I am guessing then :
imageView.setImageBitmap(bitmap);
should set the right bitmap.
Create a view of barcode and text below it and take that view as a bitmap just like a screenshot.
public Bitmap getBitmapFromView(View view) {
view.setDrawingCacheEnabled(true);
Bitmap returnedBitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
Bitmap.createScaledBitmap(returnedBitmap, img.getWidth(), img.getHeight(), false);
return returnedBitmap;
}
I'm using the following method to add a watermark logo on top of an existing bitmap:
private Bitmap addWaterMark(Bitmap src) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
Bitmap waterMark = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.menu_logo);
canvas.drawBitmap(waterMark, 0, 0, null);
return result;
}
How can I modify the above code to position the watermark in the bottom right of the bitmap, and to also make its width 33% of the bitmap, while maintaining its aspect ratio?
Ended up getting it to work how I wanted like this:
private Bitmap addWaterMark(Bitmap src) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
int watermarkPadding = 50;
Bitmap waterMark = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.menu_logo);
int newWatermarkWidth = w / 3;
int newWatermarkHeight = (waterMark.getHeight() * newWatermarkWidth) / waterMark.getWidth();
Bitmap newWatermark = Bitmap.createScaledBitmap(waterMark, newWatermarkWidth, newWatermarkHeight, true);
canvas.drawBitmap(newWatermark, w - newWatermarkWidth - watermarkPadding, h - newWatermarkHeight - watermarkPadding, null);
return result;
}
Hope this helps.
private Bitmap addWaterMark(Bitmap src) {
int w = src.getWidth();
int h = src.getHeight();
Bitmap result = Bitmap.createBitmap(w, h, src.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(src, 0, 0, null);
float scale = 0.33f;
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
matrix.postTranslate(w - w * scale, h - h * scale);
Bitmap waterMark = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.menu_logo);
canvas.drawBitmap(waterMark, matrix, null);
return result;
}
You can use this library, you can draw you watermark in XML (view) and convert to Bitmap and add on your original.
private Bitmap generateWaterMark(Bitmap src) {
//src is your original image
WaterMark waterMark = new WaterMark(mContext);
//return the your original image with watermark added
return waterMark.getImageWaterMarkFromView(src, R.layout.watermark_all);
}
I have an image of a floor plan displayed on screen, My Question how do I overlay another image on that.
see the image from another thread where I asked a how to here
/**
* floor plan drawing.
*
* #param canvas the canvas on which the background will be drawn
*/
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
image= Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
canvas.drawBitmap(image, 0, 0, null);
}
What about adding the second image over the first one?
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Bitmap temp = BitmapFactory.decodeResource(getResources(), R.drawable.floorplan);
image = Bitmap.createScaledBitmap(temp, canvas.getWidth(), canvas.getHeight(), true);
canvas.drawBitmap(image, 0, 0, null);
Bitmap over = BitmapFactory.decodeResource(getResources(), R.drawable.overlay);
image = Bitmap.createScaledBitmap(over, canvas.getWidth(), canvas.getHeight(), true);
canvas.drawBitmap(image, 0, 0, null);
}
This is my method, to overlay two images into an ImageView:
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Using this method:
public static Bitmap overlayBitmap(Bitmap bitmapBackground, Bitmap bitmapImage) {
int bitmap1Width = bitmapBackground.getWidth();
int bitmap1Height = bitmapBackground.getHeight();
int bitmap2Width = bitmapImage.getWidth();
int bitmap2Height = bitmapImage.getHeight();
float marginLeft = (float) (bitmap1Width * 0.5 - bitmap2Width * 0.5);
float marginTop = (float) (bitmap1Height * 0.5 - bitmap2Height * 0.5);
Bitmap overlayBitmap = Bitmap.createBitmap(bitmap1Width, bitmap1Height, bitmapBackground.getConfig());
Canvas canvas = new Canvas(overlayBitmap);
canvas.drawBitmap(bitmapBackground, new Matrix(), null);
canvas.drawBitmap(bitmapImage, marginLeft, marginTop, null);
return overlayBitmap;
}
get the references and bitmaps to make de overlay!
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.background);
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.androide);
Bitmap bmpImages = overlayBitmap(background, image);
imageView.setImageBitmap(bmpImages);
to have this as a result:
Download the complete sample.
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'm trying to save as an image the contents of a WebView with a canvas drawing on top of it. I've tried two methods:
Picture picture = drawView.capturePicture();
Bitmap bmp = pictureDrawable2Bitmap(new PictureDrawable(picture));
MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap,
"image" + ".png", "drawing");
private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable){
Bitmap bitmap = Bitmap.createBitmap(
pictureDrawable.getIntrinsicWidth() ,pictureDrawable.getIntrinsicHeight(),Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}
This gets me the entire WebView page but without the Canvas drawing.
Method 2:
Bitmap bitmap = Bitmap.createBitmap(drawView.getWidth(), drawView.getHeight(),
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawView.draw(canvas);
MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap,
"image" + ".png", "drawing");
drawView.destroyDrawingCache();
This way I get both the canvas and WebView but it only captures it at the current zoom level. Which means if I save it while zoomed in it will only save the current view state, not the entire image. It's also lower quality.
Any suggestion on how to get them together?
Some of the code:
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = Bitmap.createBitmap(w + 1500, h + 1500, Bitmap.Config.ARGB_8888);
drawCanvas = new Canvas(canvasBitmap);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
clipBounds = canvas.getClipBounds();
canvas.save();
//canvas.translate(clipBounds.left, clipBounds.top);
drawPaint.setStrokeWidth(8/mScaleFactor);
canvas.scale(mScaleFactor, mScaleFactor, 0, 0);
if(!pathsDrawn) {
canvas.drawPath(drawPath, drawPaint);
pathsDrawn = true;
}
canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
canvas.restore();
}
WebView settings:
final DrawingView drawView;
drawView = (DrawingView) findViewById(R.id.pdf);
drawView.setDrawingCacheEnabled(true);
WebSettings webSettings = drawView.getSettings();
webSettings.setLoadWithOverviewMode(true);
webSettings.setUseWideViewPort(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDefaultZoom(ZoomDensity.FAR);
webSettings.setSupportZoom(true);
webSettings.setDisplayZoomControls(false);
String data = "someurl.jpg"
drawView.loadUrl(data);
drawView.setDrawingCacheEnabled(true);
Picture picture = drawView.capturePicture();
Bitmap bmp = pictureDrawable2Bitmap(new PictureDrawable(picture));
MediaStore.Images.Media.insertImage(getContentResolver(), overlayMark(bmp, DrawingView.canvasBitmap),
"dfdsf" + ".png", "drawing");
EDIT*
Got it working for the most part. I basically drew two bitmaps with canvas. However, the web image bitmap and the drawing bitmap do not scale correctly. I had to offset this by scaling the drawing using a Matrix and redrawing the bitmap before overlaying it on the web bitmap. For some reason, scale it to 1.5f makes it line up perfectly, at least on the 10in tablet I'm testing it on. If anyone has any more insight on this or how to make it better let me know.
Matrix matrix = new Matrix();
matrix.postScale(1.5f, 1.5f);
Picture picture = drawView.capturePicture();
Bitmap bmp = pictureDrawable2Bitmap(new PictureDrawable(picture));
Bitmap resizedBitmap = Bitmap.createBitmap(DrawingView.canvasBitmap, 0, 0, DrawingView.canvasBitmap.getWidth(), DrawingView.canvasBitmap.getHeight(), matrix, false);
MediaStore.Images.Media.insertImage(getContentResolver(), overlayMark(bmp, resizedBitmap),
"dfdsf" + ".png", "drawing");
private static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable){
Bitmap bitmap = Bitmap.createBitmap(
pictureDrawable.getIntrinsicWidth() ,pictureDrawable.getIntrinsicHeight(),Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawPicture(pictureDrawable.getPicture());
return bitmap;
}
private Bitmap overlayMark(Bitmap bmp1, Bitmap bmp2) {
int bh = bmp1.getHeight();
int bw = bmp1.getWidth();
Bitmap bmOverlay = Bitmap.createBitmap(bw,bh,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
}