如何通过在java中编程合并android中的两个图像并保存在外部SD卡或其他地方。在Android中合并图像
回答
尝试这个代码。
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"/>
试试下面的代码
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;
}
嗨,我在这段代码中显示错误,它显示位图是不可变的?这是什么。谢谢 – Herry 2011-05-10 12:30:14
哪里说哪一行,你准确使用这段代码? – ingsaurabh 2011-05-10 12:33:20
在画布线显示错误任何方式感谢我从这个链接得到答案[http://ketankantilal.blogspot.com/2011/03/how-to-combine-images-and-store-to.html感谢您的帮助。 – Herry 2011-05-10 12:35:42
- 1. Android合并两张图像,另一张合并两张图像
- 2. 如何在android中组合(合并)两张照片图像?
- 3. 合并图像
- 4. Android - 如何合并文字和图像?
- 5. Qt图像合并
- 6. iphone图像合并
- 7. 合并图像php
- 8. 在Android(小部件)中合并两个/更多图像
- 9. 在GD中使用php合并图像
- 10. 在Java中合并图像FX
- 11. 在C#/。NET中合并两个图像
- 12. 在“UITextView”中合并文本和图像
- 13. 在React Native中合并多个图像
- 14. 在GD中使用php合并图像
- 15. 在php中合并两个图像
- 16. 图像合并在旋转木马中
- 17. Android,将图像和文本视图合并到屏幕中
- 18. 在Android中结合两个图像java
- 19. 在android中合并两个位图
- 20. 在Android中合并图片与视频
- 21. 通过拖放合并(合并)图像
- 22. 在Android中触摸并拖动图像
- 23. 如何在php中的背景PNG图像合并图像
- 24. 将图像与图像合并
- 25. 如何在将图像合并到其他图像时将图像居中?
- 26. 合并多个图像转换成一个在水平的Android
- 27. Android中的ImageButton中的适合图像
- 28. 合并两个图像
- 29. 用VB.Net合并大图像
- 30. 如何合并图像?
你到底通过合并两个图像是什么意思? – 2011-05-10 10:52:23
我有两个不同的图像,我想在android中制作程序,通过编程将这些图像合并为一个图像。 – Herry 2011-05-10 11:11:26
再次说明你是什么意思的结合。如果您有两幅图像需要生成单幅图像,那么这两幅图像是两者的拼接,或者您想要以某种方式对像素值进行求和。如果第一张图像比第二张更大,请详细解释 – 2011-05-10 11:18:55