安卓:保存帆布为JPEG / PNG SD卡上 [英] Android: Saving a canvas as a jpeg/png on SD card
问题描述
我有2张图像,一个我从我的摄像机视图得到的,另一种是在摄像头视图。
我要救我的SD卡上的这2图像的组合。我想我必须的Zork zith画布,但我不知道如何保存帆布成JPEG,即从画布中的数据写入到一个FileOutputStream
I have 2 images, one I get from my camera view, the other is upon the camera view. I want to save the combination of these 2 images on my SD card. I think I have to zork zith canvas but I don't know how to save a canvas into jpeg i.e. write the data from canvas into a FileOutputStream
下面是我的code
output = new File(imagesFolder, fileName);
ImageView view = (ImageView) gameactivity.findViewById(R.id.imageView3);
view.setDrawingCacheEnabled(true);
Bitmap bitmap2 = view.getDrawingCache();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(output);
fos.write(data);
fos.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
try {
Bitmap bitmap = BitmapFactory.decodeFile(output.getAbsolutePath());
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap2, null, null);
// HERE I HAVE TO SAVE THE CANVAS INTO JPEG
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
你能告诉我,如果这是一个很好的开始时?如何解决我的问题,我不觉得对堆栈中的类似的帖子很好的答案(因为我不希望我的画到画布视图)
Could you please tell me if that is a good begining? and how to solve my problem, I don't find good answers on the similar post on stack (as I don't want to draw my canvas into view)
推荐答案
画布只是提请位图的一种手段。
Canvas is just a means to draw to the Bitmap.
您应该已经创建了帆布与新的Canvas(MYBITMAP);
。所以,当你在画布上画画,它吸引你的位图。
You should have created Canvas with new Canvas(myBitmap);
. So when you draw on the Canvas, it draws to your bitmap.
因此,使用 MYBITMAP
。
String fileName = Environment.getExternalStorageDirectory() + "/test.png";
OutputStream stream = new FileOutputStream(fileName);
/* Write bitmap to file using JPEG or PNG and 80% quality hint for JPEG. */
myBitmap.compress(CompressFormat.PNG, 80, stream);
stream.close();
这篇关于安卓:保存帆布为JPEG / PNG SD卡上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!