在android中合并图像

发布于 2024-11-05 19:46:00 字数 47 浏览 0 评论 0原文

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁以何悠 2024-11-12 19:46:00

尝试下面的代码

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;
}

Try below code

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;
}
吾性傲以野 2024-11-12 19:46:00

试试这个代码。

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;

Manifest中的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

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 Manifestadd this uses-permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文