我有一个基础图像视图,可以从SD卡加载图像。用户可以在单击按钮时将模板(位图图像)添加到此图像视图上,并可以将添加的图像定位到用户需要的位置。我想将这些多个图像保存为单个图像到SD卡上。我该如何做到这一点?
imageview.setDrawingCacheEnabled(true);
Bitmap b = imageview.getDrawingCache();
b.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/image.jpg"));
你可能需要在清单文件中设置SD卡的写入权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
看一下这个代码片段:
int rows = 2; //we assume the no. of rows and cols are known and each chunk has equal width and height
int cols = 2;
int chunks = rows * cols;
int chunkWidth, chunkHeight;
int type;
//fetching image files
File[] imgFiles = new File[chunks];
for (int i = 0; i < chunks; i++) {
imgFiles[i] = new File("archi" + i + ".jpg");
}
//creating a bufferd image array from image files
BufferedImage[] buffImages = new BufferedImage[chunks];
for (int i = 0; i < chunks; i++) {
buffImages[i] = ImageIO.read(imgFiles[i]);
}
type = buffImages[0].getType();
chunkWidth = buffImages[0].getWidth();
chunkHeight = buffImages[0].getHeight();
//Initializing the final image
BufferedImage finalImg = new BufferedImage(chunkWidth*cols, chunkHeight*rows, type);
int num = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
finalImg.createGraphics().drawImage(buffImages[num], chunkWidth * j, chunkHeight * i, null);
num++;
}
}
System.out.println("Image concatenated.....");
ImageIO.write(finalImg, "jpeg", new File("finalImg.jpg"));