在android中组合图像 [英] Combine Images in android

查看:97
本文介绍了在android中组合图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过java编程在android中合并两个图像并保存在外部SD卡或其他地方。

how to merge two images in android by programming in java and save in external SD Card or some where else.

推荐答案

试试这段代码。

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

清单添加此uses-permission < uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/>

in Manifestadd this uses-permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

这篇关于在android中组合图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆