在Eclipse中,我有来自模拟器的捕获图像,它将格式保存为JPEG。但是我想将图像格式保存为PNG。如何将图像格式从JPEG转换为PNG。
这里我使用此代码将图像保存在目录中。
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".PNG";
// String photoFile = "Picture_" + date + ".JPEG";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
File pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(context, "New Image saved:" + photoFile,
Toast.LENGTH_LONG).show();
} catch (Exception error) {
//Log.d(IntersaActivity.DEBUG_TAG, "File" + filename + "not saved: "+ error.getMessage());
Toast.makeText(context, "Image could not be saved.",
Toast.LENGTH_LONG).show();
}
先谢谢。
答案 0 :(得分:0)
这里是使用Bitmap类
创建图像文件的代码修改强>
您需要先将现有的图像文件打开到此bmp对象中,然后在文件对象中为保存提供另一个名称。
Bitmap bmp = BitmapFactory.decodeFile(pathName);// here you need to pass your existing file path
File image = new File(your_sdcard_file_path);
FileOutputStream outStream;
try {
image.createFile(); // need to create file as empty first if it was exist already then change the name
outStream = new FileOutputStream(image);
// here you need to pass the format as I have passed as PNG so you can create the file with specific format
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
/* 100 to keep full quality of the image */
outStream.flush();
outStream.close();
success = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}