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 =)
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);
Android - combine multiple images into one ImageView
You can use MultiImageView.
Add dependency in app.gradle:
compile 'com.github.stfalcon:multiimageview:0.1'
Add MultiImageView to layout xml file
<com.stfalcon.multiimageview.MultiImageView
android:id="@+id/iv"
android:layout_width="100dp"
android:layout_height="100dp"/>
In java class find view by id:
final MultiImageView multiImageView = (MultiImageView) findViewById(R.id.iv);
For adding image to MultiImageView use method addImage(Bitmap bitmap). For exapple:
multiImageView.addImage(BitmapFactory.decodeResource(getResources(), R.drawable.avatar1));
For setting shape of MultiImageView use method setShape(MultiImageView.Shape shape).
multiImageView.setShape(MultiImageView.Shape.RECTANGLE);//Rectangle with round corners
multiImageView.setShape(MultiImageView.Shape.CIRCLE);//Circle
multiImageView.setShape(MultiImageView.Shape.NONE);//Without shape
Check github link for more information:
I think it is what you needed
Merge multiple images into single image
You can get an image for the whole view hierarchy using the drawing cache :
public Bitmap createBitmapFromView(View v) {
v.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return bitmap;
}
The Bitmap
can be saved, or used in a BitmapDrawable
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)
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"/>
Merging multiple picture to form one complete picture in Android
You could merge the images to a bigger images by drawing the small images to a canvas associated with the resulting big bitmap
Canvas c=new Canvas(result_bitmapenter);
and then draw your small images onto the canvas
c.drawBitmap(small,...);
But that might not be the best way as big images eat lots of memory - perhaps you should concider dynamic loading instead of merging then
Related Topics
The Application Icon Does Not Show on Action Bar
How to Block Virtual Keyboard While Clicking on Edittext in Android
Android Google+ Integration - Repeated Userrecoverableauthexception
Justify Text in an Android App Using a Webview But Presenting a Textview-Like Interface
React Native Adb Reverse Enoent
How to Play Audio File in Android
Android Studio Doesn't Recognize My Device
How to Set My Sms App Default in Android Kitkat
How to Close All the Activities of My Application
How to Make Proguard Ignore External Libraries
Retrieve Large Blob from Android SQLite Database
Check If the Sdcard Is Present, Boolean Is Always True
Load the Image Saved in Sdcard in Webview
Mediaplayer Error (1, -1004) Aka Media_Error_Io Trying to Stream Music on Samsung S3