combining two png files in android
I've been trying to figure this out for a little while now.
Here's (essentially) the code I used to make it work.
// Get your images from their files
Bitmap bottomImage = BitmapFactory.decodeFile("myFirstPNG.png");
Bitmap topImage = BitmapFactory.decodeFile("myOtherPNG.png");
// As described by Steve Pomeroy in a previous comment,
// use the canvas to combine them.
// Start with the first in the constructor..
Canvas comboImage = new Canvas(bottomImage);
// Then draw the second on top of that
comboImage.drawBitmap(topImage, 0f, 0f, null);
// comboImage is now a composite of the two.
// To write the file out to the SDCard:
OutputStream os = null;
try {
os = new FileOutputStream("/sdcard/DCIM/Camera/" + "myNewFileName.png");
comboImage.compress(CompressFormat.PNG, 50, os)
} catch(IOException e) {
e.printStackTrace();
}
EDIT :
there was a typo,
So, I've changed
image.compress(CompressFormat.PNG, 50, os)
to
bottomImage.compress(CompressFormat.PNG, 50, os)
Merging Two Images into one Image
If you want to create a side-by-side merged image you will need to create a result bitmap with 2 times the width of first image, or, more scalably, the sum of the widths of the images:
Currently, you are creating a result image with width firstImage.getWidth()
. They will clearly overlap or be off the canvas.
Also, you will need to place the second image at x == firstImage.getWidth()
Check out this code (untested):
private Bitmap createSingleImageFromMultipleImages(Bitmap firstImage, Bitmap secondImage) {
Bitmap result = Bitmap.createBitmap(firstImage.getWidth() + secondImage.getWidth(), firstImage.getHeight(), firstImage.getConfig());
Canvas canvas = new Canvas(result);
canvas.drawBitmap(firstImage, 0f, 0f, null);
canvas.drawBitmap(secondImage, firstImage.getWidth(), 0f, null);
return result;
}
Bitmap mergedImages = createSingleImageFromMultipleImages(firstImage, secondImage);
im.setImageBitmap(mergedImages);
Merging png files programmatically
2 days of trial and error finally hit on the solution.
No need to compress the file or write any new file or use any bitmap as a template and no need to reference the new drawable as a resource (instead just apply the created Bitmap variable) were the issues here.
Bitmap builtFirst = Bitmap.createBitmap(500 * splitFirst.length, 700, ARGB_8888);
Canvas canvas = new Canvas(builtFirst);
if (buildFirst.length > 1) {
for (int i = 0; i < buildFirst.length; i++) {
canvas.drawBitmap(buildFirst[i], 300 * i, 0, null);
canvas.drawBitmap(builtFirst, new Matrix(), null);
}
}
imageViewFirstNumber.setImageBitmap(builtFirst);
Combining 2 Images overlayed
For overlaying two bitmaps:
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 for scaling one first you should check out createScaledBitmap, e.g:
Bitmap scaledBitmap = Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter);
Android merge two images
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.
Combining two bitmap image (side by side)
You can use Canvas
- check out this article:
http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas
Updated code to do it side by side:
public Bitmap combineImages(Bitmap c, Bitmap s) { // 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 cs = null;
int width, height = 0;
if(c.getWidth() > s.getWidth()) {
width = c.getWidth() + s.getWidth();
height = c.getHeight();
} else {
width = s.getWidth() + s.getWidth();
height = c.getHeight();
}
cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas comboImage = new Canvas(cs);
comboImage.drawBitmap(c, 0f, 0f, null);
comboImage.drawBitmap(s, c.getWidth(), 0f, 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);
cs.compress(CompressFormat.PNG, 100, os);
} catch(IOException e) {
Log.e("combineImages", "problem combining images", e);
}*/
return cs;
}
Combine Images in android
Try out this code.
private static final String TAG = "JoinImage";
private Bitmap mBackImage, mTopImage, mBackground;
private BitmapDrawable mBitmapDrawable;
private static String mTempDir;
private String mSavedImageName = null;
private FileOutputStream mFileOutputStream = null;
private Canvas mCanvas;
in onCreate()
//Create folder in SDCard to store newly generated image
mTempDir = Environment.getExternalStorageDirectory() + "/TestTemp/";
File mTempFile = new File(mTempDir);
if(!mTempFile.exists()) {
mTempFile.mkdirs();
}
//File name
mSavedImageName = "Test.png";
//Width = 604, Height = 1024 Change as per your requirement
mBackground = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
//Put back and top images in your res folder
mBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.launcher);
mTopImage = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
mCanvas = new Canvas(mBackground);
mCanvas.drawBitmap(mBackImage, 0f, 0f, null);
mCanvas.drawBitmap(mTopImage, 12f, 12f, null);
try {
mBitmapDrawable = new BitmapDrawable(mBackground);
Bitmap mNewSaving = mBitmapDrawable.getBitmap();
String FtoSave = mTempDir + mSavedImageName;
File mFile = new File(FtoSave);
mFileOutputStream = new FileOutputStream(mFile);
mNewSaving.compress(CompressFormat.PNG, 95, mFileOutputStream);
mFileOutputStream.flush();
mFileOutputStream.close();
} catch(FileNotFoundException e) {
Log.e(TAG, "FileNotFoundExceptionError " + e.toString());
} catch(IOException e) {
Log.e(TAG, "IOExceptionError " + e.toString());
}
Log.i(TAG, "Image Created");
in Manifest
add this uses-permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
How to combine multiple images into a single image in android?
Bitmap[] parts = new Bitmap[4];
Bitmap result = Bitmap.createBitmap(parts[0].getWidth() * 2, parts[0].getHeight() * 2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
for (int i = 0; i < parts.length; i++) {
canvas.drawBitmap(parts[i], parts[i].getWidth() * (i % 2), parts[i].getHeight() * (i / 2), paint);
}
Something like this =)
Related Topics
List View Item Swipe Left and Swipe Right
How to Make an Android View That Flips Between Views on Swipe/Fling
Set Visibility of Progress Bar Gone on Completion of Image Loading Using Glide Library
Isvalidfragment Android API 19
Listfragment Does Not Accept My Layout
Check for Access to Notifications Using Notificationlistenerservice
Change The Background Color of Cardview Programmatically
Redirect Users to Itunes App Store or Google Play Store
Detecting Webview Error and Show Message
Firebase Console: How to Specify Click_Action for Notifications
How to Be Notified When a Snackbar Has Dismissed Itself
Fragmentmanager Is Already Executing Transactions. When Is It Safe to Initialise Pager After Commit
View Pager + Imageview +Pinch Zoom + Rotation
Difference Between Motionevent.Getrawx and Motionevent.Getx