本文介绍了保存捕捉的图像以JPEG格式的PNG的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的Android。我使用Eclipse的一个摄像头应用程序。拍摄的图像存储为应用程序文件夹(内部存储)。将图像保存为JPEG格式,但我希望它被保存为PNG格式。不过,我不希望保存的图像外部存储目录
下面是我的code:
的SimpleDateFormat DATEFORMAT =新的SimpleDateFormat(YYYYMMDDHHMMSS);
字符串日期= dateFormat.format(新的Date());
字符串photoFile =Picture_+日期+.JPEG 字符串文件名= pictureFileDir.getPath()+文件分割符+ photoFile;
文件PictureFile的=新的文件(文件名);
尝试{
FOS的FileOutputStream =新的FileOutputStream(PictureFile的);
bmp.com preSS(Bitmap.Com pressFormat.PNG,100,FOS); fos.flush();
// fos.write(数据);
fos.close();
Toast.makeText(背景下,新形象保存:+ photoFile,Toast.LENGTH_LONG).show();
}赶上(例外错误){
//Log.d(IntersaActivity.DEBUG_TAG,文件+文件名+不保存+ error.getMessage());
Toast.makeText(背景下,图像无法被保存。
Toast.LENGTH_LONG).show();
}
} 私人文件GETDIR(){
字符串的文件路径=MyFileStorage;
ContextWrapper contextWrapper =新ContextWrapper(背景);
文件sdDir = contextWrapper.getDir(文件路径,Context.MODE_PRIVATE);
//文件sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
返回新的文件(sdDirCameraAPIDemo);
}
}
解决方案
使用
私人无效savePicture(字符串文件名,位图B,上下文CTX){
尝试{
ObjectOutputStream的OOS;
FileOutputStream中出; // =新的FileOutputStream(文件名);
OUT = ctx.openFileOutput(文件名,Context.MODE_PRIVATE);
OOS =新的ObjectOutputStream(出);
b.com preSS(Bitmap.Com pressFormat.PNG,100,OOS); oos.close();
oos.notifyAll();
out.notifyAll();
out.close();
}赶上(例外五){
e.printStackTrace();
}
}
和设置文件名的扩展名是PNG
希望它帮助。
编辑:您的总code。与调用此方法
的SimpleDateFormat DATEFORMAT =新的SimpleDateFormat(YYYYMMDDHHMMSS);
字符串日期= dateFormat.format(新的Date());
字符串photoFile =Picture_+日期+.PNG字符串文件名= pictureFileDir.getPath()+文件分割符+ photoFile;尝试{
savePicture(文件名,BMP,背景);
Toast.makeText(背景下,新形象保存:+ photoFile,Toast.LENGTH_LONG).show();
}赶上(例外错误){
Toast.makeText(背景下,图像无法保存,Toast.LENGTH_LONG).show();
}
I am new to Android. I am using a camera application in Eclipse. The captured image is stored to APPLICATION FOLDER(INTERNAL STORAGE). The image is stored as JPEG Format, but I want it to be save as PNG Format. However, I don't want to save the image on EXTERNAL STORAGE DIRECTORY.
Here is my code :
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".JPEG";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
File pictureFile = new File(filename);
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
bmp.compress(Bitmap.CompressFormat.PNG,100,fos);
fos.flush();
// 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();
}
}
private File getDir() {
String filepath = "MyFileStorage";
ContextWrapper contextWrapper = new ContextWrapper(context);
File sdDir = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
//File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return new File(sdDir, "CameraAPIDemo");
}
}
解决方案
Use
private void savePicture(String filename, Bitmap b, Context ctx){
try {
ObjectOutputStream oos;
FileOutputStream out;// = new FileOutputStream(filename);
out = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
oos = new ObjectOutputStream(out);
b.compress(Bitmap.CompressFormat.PNG, 100, oos);
oos.close();
oos.notifyAll();
out.notifyAll();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
And set the extension of the file name to be PNG
Hope it helps.
EDIT: Your total code with calls to this method.
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date());
String photoFile = "Picture_" + date + ".PNG";
String filename = pictureFileDir.getPath() + File.separator + photoFile;
try {
savePicture(filename, bmp, context);
Toast.makeText(context, "New Image saved:" + photoFile,Toast.LENGTH_LONG).show();
} catch (Exception error) {
Toast.makeText(context, "Image could not be saved.", Toast.LENGTH_LONG).show();
}
这篇关于保存捕捉的图像以JPEG格式的PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!