在安卓系统中合并图片

5
如何在Android中使用Java编程合并两个图像并保存到外部SD卡或其他位置。

你说的合并两张图片是什么意思? - Mojo Risin
我有两张不同的图片,我想在Android上编写一个程序,通过编程将这些图片合并为一张图片。 - Herry
1
再次问一下,您所说的“合并”是什么意思?如果您有两个图像,您想要生成一个由这两个图像串联而成的单个图像,还是您想以某种方式对像素值进行求和?如果第一个图像比第二个图像大?请详细说明。 - Mojo Risin
Ketan的答案链接已经失效。 - Chirag
抱歉,看起来他的博客已经被删除了。 - Herry
@ChiragRaval 我已经发布了代码。 - user456118
2个回答

8

尝试以下代码

private Bitmap joinImages(File first, File second)
{
    Bitmap bmp1, bmp2;
    bmp1 = BitmapFactory.decodeFile(first.getPath());
    bmp2 = BitmapFactory.decodeFile(second.getPath());
    if (bmp1 == null || bmp2 == null)
        return bmp1;
    int height = bmp1.getHeight();
    if (height < bmp2.getHeight())
        height = bmp2.getHeight();

    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth() + bmp2.getWidth(), height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, 0, 0, null);
    canvas.drawBitmap(bmp2, bmp1.getWidth(), 0, null);
    return bmOverlay;
}

在哪一行上写着这段代码?你是否完全使用了这段代码? - ingsaurabh
在画布线上显示错误,不管怎样感谢我从这个链接[http://ketankantilal.blogspot.com/2011/03/how-to-combine-images-and-store-to.html]得到了答案,谢谢帮助。 - Herry
1
是的,我们看到了Ketan的代码,并发现我们需要将Bitmap.Config.ARGB_8888这个属性传递给我们在后台设置的位图,这解决了我的问题。感谢您的帮助。 - Herry
实际上问题在于您尝试连接的位图是不可变的,因此之前的代码正在获取您提供的位图的配置。 - ingsaurabh

3

尝试这段代码。

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;

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");

Manifest中添加以下<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>以获取写入外部存储的权限。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接