如何通過在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. 在JavaScript中合併圖像
- 3. 如何在android中組合(合併)兩張照片圖像?
- 4. 合併圖像
- 5. Android - 如何合併文字和圖像?
- 6. 合併base64圖像
- 7. iphone圖像合併
- 8. 合併圖像php
- 9. PHP合併圖像
- 10. Qt圖像合併
- 11. 如何使用Canvas在Android中合併兩個圖像?
- 12. 在Android(小部件)中合併兩個/更多圖像
- 13. 在GD中使用php合併圖像
- 14. 在Java中合併圖像FX
- 15. 在GD中使用php合併圖像
- 16. 在winRT中合併2張圖像
- 17. 在C#/。NET中合併兩個圖像
- 18. 圖像合併在旋轉木馬中
- 19. 在「UITextView」中合併文本和圖像
- 20. 在php中合併兩個圖像
- 21. 在React Native中合併多個圖像
- 22. 在php中合併兩個圖像
- 23. 在PHP中合併透明圖像
- 24. Android,將圖像和文本視圖合併到屏幕中
- 25. 在Android中結合兩個圖像java
- 26. 在Android中合併圖片與視頻
- 27. 在android中合併兩個位圖
- 28. 通過拖放合併(合併)圖像
- 29. 在WPF中將png圖像合併爲單個圖像
- 30. 如何在php中的背景PNG圖像合併圖像
你到底通過合併兩個圖像是什麼意思? – 2011-05-10 10:52:23
我有兩個不同的圖像,我想在android中製作程序,通過編程將這些圖像合併爲一個圖像。 – Herry 2011-05-10 11:11:26
再次說明你是什麼意思的結合。如果您有兩幅圖像需要生成單幅圖像,那麼這兩幅圖像是兩者的拼接,或者您想要以某種方式對像素值進行求和。如果第一張圖像比第二張更大,請詳細解釋 – 2011-05-10 11:18:55