在android中组合图像
如何在android中通过java编程合并两个图像并保存在外部SD卡或其他地方。尝试下面的代码在android中组合图像,android,image,bitmap,Android,Image,Bitmap,如何在android中通过java编程合并两个图像并保存在外部SD卡或其他地方。尝试下面的代码 private Bitmap joinImages(File first, File second) { Bitmap bmp1, bmp2; bmp1 = BitmapFactory.decodeFile(first.getPath()); bmp2 = BitmapFactory.decodeFile(second.getPath()); if (bmp1 == n
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;
}
私有位图图像(文件第一,文件第二)
{
位图bmp1、bmp2;
bmp1=BitmapFactory.decodeFile(first.getPath());
bmp2=BitmapFactory.decodeFile(second.getPath());
如果(bmp1==null | | bmp2==null)
返回bmp1;
int height=bmp1.getHeight();
if(高度
试试这个代码
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()中
在Manifest
addthis uses permission
中,合并两个图像的确切含义是什么?我有两个不同的图像,我想在android中制作一个程序,通过编程将这些图像合并为一个图像。再一次,合并是什么意思。如果您有两个图像,您希望生成两个图像串联的单个图像,或者希望以某种方式求和像素值。如果第一个图像比第二个图像大?请更详细地解释Sketan的答案链接已断开。很抱歉,看起来他的博客已被删除。它在哪一行上显示,您正在准确地使用此代码?在画布行上,它显示任何方式的错误谢谢我从此链接获得答案[谢谢您的帮助。是的,我们看到了Ketan的代码,并发现我们需要将Bitmap.Config.ARGB_8888此属性传递到位图,我们在后台设置了该属性,它解决了我的问题。谢谢您的帮助。实际上,问题是您尝试加入的位图是不可变的,这就是为什么代码之前会获得您正在使用的位图的配置撒谎
//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");