繁体   English   中英

Android:在内部存储目录中将捕获的图像格式从JPEG保存为PNG

[英]Android:save captured image format from JPEG to PNG in internal storage directory

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

提前致谢。

这里是使用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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM