Android merge two images - android

I have these two images, which I basically merge on canvas. Now i want to save that canvas into an image. How should i do it or if there is any other way to merge two images.
My sample code is -
Bitmap bmp1 = BitmapFactory.decodeResource(getResources(),
R.drawable.duckpic);
Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),
R.drawable.img);
// canvas.drawColor(Color.BLACK);
// canvas.drawBitmap(_scratch, 10, 10, null);
Bitmap bmOverlay = Bitmap.createBitmap(bmp2.getWidth(), bmp2
.getHeight(), bmp2.getConfig());
// Canvas cs = new Canvas(bmp2);
canvas.scale((float) 0.5, (float) 0.5);
canvas.drawBitmap(bmp2, new Matrix(), null);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.save();
I got it working by doing this -
cs = Bitmap.createBitmap(c.getWidth(), c.getHeight(), Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(s, new Matrix(), null);
comboImage.drawBitmap(c, new Matrix(), null);
comboImage.save();
// this is an extra bit I added, just incase you want to save the new
// image somewhere and then return the location
String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
OutputStream os = null;
try {
os = new FileOutputStream("/sdcard/" + tmpImg);
cs.compress(CompressFormat.PNG, 100, os);
} catch (IOException e) {
Log.e("combineImages", "problem combining images", e);
}
Basically it is given here - http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas

Use canvas.setBitmap(Bitmap bitmap). This will send the canvas to the specified Bitmap. You'll want to create a new, mutable bitmap for this. After you call setBitmap you can then save that Bitmap to a file.

Related

Generation of 2D-ortho-mosaic image in Android

I am developing an Android application for image processing. In that, I first need to generate an ortho image by stitching 100+ images together. I have tried it in Android using Bitmap and Canvas. I first did it for 3 images. Is it the right way to generate an ortho or is there any better solution for this? Here is the code that I have used for stitching 3 images:
private void joinImages(File first, File second , File third) throws IOException
{
Bitmap bmp1, bmp2, bmp3;
bmp1 = BitmapFactory.decodeFile(first.getPath());
bmp2 = BitmapFactory.decodeFile(second.getPath());
bmp3 = BitmapFactory.decodeFile(third.getPath());
int height = bmp1.getHeight()+bmp2.getHeight()+bmp3.getHeight();
String height1 = height+"";
Log.d(height1,"heght");
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("/storage/emulated/0/DCIM/final.jpg");
bmOverlay.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.close();
}
As I am new to Android as well as image processing, I also want to know the difference between image stitching and 2D-orthomosaic generation. Kindly help me with this issue.

Merge two images one below the other in android by adding pixels

I am trying to join images and form 1 single image.This is done to send the image to the server.
//Obtain the bitmaps from drawabl folder
Bitmap bm1 = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Bitmap bm2 = BitmapFactory.decodeResource(getResources(), R.drawable.img);
//Create a buffer
ByteBuffer buffer3 = ByteBuffer.allocate((bm1.getHeight()+bm2.getHeight()) * (bm1.getRowBytes()+bm2.getRowBytes()));
//copy the pixels to buffer
bm2.copyPixelsToBuffer(buffer3);
bm1.copyPixelsToBuffer(buffer3);
//Covert to byteArray
byte[] bytes = buffer3.array();
int leftovers = buffer3.remaining();
buffer3.compact();
//Finally forming a bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes , 0, bytes.length);
ImageView img = (ImageView) findViewById(R.id.imgV);
img.setImageBitmap(bitmap);
But the problem I am facing is my "bitmap" is coming as null.
Can anyone please help me joining images.
In this case you can use canvas like as follows
public Bitmap mergeBitmap(Bitmap bitmap1, Bitmap bitmap2) {
Bitmap mergedBitmap = null;
int w, h = 0;
h = bitmap1.getHeight() + bitmap2.getHeight();
if (bitmap1.getWidth() > bitmap2.getWidth()) {
w = bitmap1.getWidth();
} else {
w = bitmap2.getWidth();
}
mergedBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mergedBitmap);
canvas.drawBitmap(bitmap1, 0f, 0f, null);
canvas.drawBitmap(bitmap2, 0f, bitmap1.getHeight(), null);
return mergedBitmap;
}

Bitmap.getPixel only returning 0

I have the following code for creating a bitmap:
public PixelMapper(Path inputPath){
RectF src = new RectF();
inputPath.computeBounds(src, true);
int width = (int)src.width()+1;
int height = (int)src.height()+1;
largeBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
Paint canvasPaint = new Paint();
canvasPaint.setAntiAlias(false);
canvasPaint.setColor(Color.BLACK);
canvasPaint.setStyle(Style.STROKE);
canvasPaint.setStrokeWidth(5);
Canvas canvas = new Canvas(largeBitmap);
canvas.drawPath(inputPath, canvasPaint);
bitmap = Bitmap.createScaledBitmap(largeBitmap, SIDE_OF_BITMAP, SIDE_OF_BITMAP, true);
bitmap = Bitmap.createBitmap(bitmap);//so that a immutable bitmap is created
drawPixelMap();
}
public void drawPixelMap(){
for(int x=0; x<bitmap.getWidth(); x++){
String msg="";
for(int y=0; y<bitmap.getHeight(); y++){
msg = Integer.toHexString( bitmap.getPixel(x,y) );
Log.v("bitmap", msg);
}
}
}
int[] pixels = new int[64];
bitmap.getPixels(pixels, 0, bitmap.getWidth(), 0, 0, SIDE_OF_BITMAP, SIDE_OF_BITMAP);
bitmap.setPixels(pixels, 0, 8, 0, 0, 8, 8);
for ( int pixel : pixels)
Log.v("bitmap", Integer.toHexString(pixel) );
The problem is that all the log messages are "0": both getPixel and getPixels return "0". What is worse is that if I remove the line bitmap.getPixels(...); and leave the bitmap.setPixels(...) line,the image is still drawn as before. It seems that the bitmap variable is just a reference and a bitmap doesn't exist, and for some reason, I am unable to get those pixels.
I know the bitmap is created as required as I am able to view it on a ImageView. It shows black and white pixels with a few grey ones too. Code:
Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 128, 128, false);
imageView1.setImageBitmap(newBitmap);
The SIDE_OF_BITMAP = 8, all classes (Path, Bitmap, Canvas) are of android.
I tried saving the bitmap to file with the following code:
public String saveToStorage(String fileName){
if( !storageDir.exists() )
storageDir.mkdirs();
File file = new File(storageDir, fileName + ".png");
try{
OutputStream out = new FileOutputStream(file);
boolean result = bitmap.compress(CompressFormat.PNG, 100, out);
out.close();
return Boolean.toString(result);
}
catch (FileNotFoundException e){
Log.e("save", "cannot save file", e);
return "File not found exception";
}
catch (IOException e){
Log.e("save", "IO error", e);
return "IO Exception";
}
}
but it returns "false" i.e. the bitmap.compress method returns false. Please give me any help at all, not necessarily sample code.
I found my mistake. It lies in the line
largeBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
The Bitmap.Config.ALPHA_8 only creates a mask. It does not create actual pixels. That is why I was getting the erroneous results. However, when I was setting it to a ImageView I was getting the required result because its background (by default) is white.
I solved my problem by changing the Bitmap.Config.ALPHA_8 to Bitmap.Config.ARGB_8888
The line
bitmap = Bitmap.createBitmap(bitmap);
creates a immutable bitmap of the same dimension/color depth, etc as the source bitmap, but not converting the source bitmap from mutable to immutable. So your new bitmap is blank.
You can first create a immutable bitmap using newBitmap = Bitmap.createBitmap(int[], int, int, Bitmap.Config), then create a canvas for it by canvas = new Canvas(newBitmap). Your bitmap can now be copied to newBitmap by canvas.drawBitmap(bitmap, 0, 0, null).

capturing image from camera and overlaying another bitmap before we save it

here tempdata is the data captured from camera, savephoto(Bitmap) is a method am using to save the image taken from camera, and it is executing accurately ,,
BUt on [2]
i am overlaying another bitmap ,, and when i am calling the savephoto(p)
it is creating an empty file in the memorycard ...
not saving any image.
how can i overlay the two bitmap on top of each other
[1]File Imgname = Environment.getExternalStorageDirectory();
Bitmap bmp = BitmapFactory.decodeByteArray(tempdata,0,tempdata.length);
imv.setImageBitmap(bmp);
savePhoto(bmp);
[2]Bitmap bmp2 = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
Bitmap b = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
canvas.drawBitmap(bmp, 0,0, null);
canvas.drawBitmap(bmp2, 50, 50, null);
savePhoto(b);
any help will be greatly appreciated
thanx
you can do like this after getting after getting bitmap from camera (assume bitmap1) and your bitmap to overlay on top of bitmap1 (assume bitmap2)
call this overlayMark() with your bitmaps it will return overlay bitmap that is your required bitmap . you can save that bitmap..
private Bitmap overlayMark(Bitmap bmp1, Bitmap bmp2) {
int bh = originalBitmap.getHeight();
int bw = originalBitmap.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;
}

Function to Overlay Bitmap not working properly

I am Using the Function to Mearge the Two Bitmap File on One Another and it also overlay.
I am using this Function to Overlay it on OneAnother.
public static Bitmap combineImages(Bitmap cameraImage, Bitmap visionImage) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom
Bitmap finalImage = null;
int width, height = 0;
width = cameraImage.getWidth();
height = cameraImage.getHeight();
finalImage = Bitmap.createBitmap(width, height, cameraImage.getConfig());
Canvas canvas = new Canvas(finalImage);
canvas.drawBitmap(cameraImage, new Matrix(), null);
canvas.drawBitmap(visionImage, new Matrix(), null);
// this is an extra bit I added, just incase you want to save the new image somewhere and then return the location
/*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png";
OutputStream os = null;
try {
os = new FileOutputStream(loc + tmpImg);
finalImage.compress(CompressFormat.PNG, 100, os);
} catch(IOException e) {
Log.e("combineImages", "problem combining images", e);
}*/
return finalImage;
}
But After saving this Image I show that images to be combine with each other. it is not overlay. I want it to be Overlay on One Another.
Please tell me where i am wrong in this Function ??
Thanks.
this is the function to overlay two bitmap,s
private Bitmap overlayMark(Bitmap bmp1, Bitmap bmp2) {
int bh = originalBitmap.getHeight();
int bw = originalBitmap.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;
}

Categories

Resources