0

在 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();
    } 

提前致谢。

4

1 回答 1

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();
}
于 2013-04-02T06:35:10.583 回答