sd_card保存后,jpeg保存失真
因此,我使用 fileOutputStream 将图片保存到 SD 卡 - 我将图像格式化为 jpeg - 但图像保存时会变形。这很奇怪,因为缩略图是图像的正确表示,但打开时,它是一系列水平线。
以下是我的代码中的一些片段:
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
if(previewing){
camera.stopPreview();
previewing=false;
}
if(camera!=null){
try{
camera.setPreviewDisplay(surfaceHolder);
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setPreviewSize(width, height);
camera.setParameters(parameters);
camera.startPreview();
previewing=true;
} catch(IOException e){}
}
}
并且
@Override
public void onPictureTaken(byte[] data, Camera camera) {
OutputStream outputStream;
try{
outputStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis())); //to sd card
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
};
图像(水平扭曲的 jpeg 和放在一起的缩略图)目前没有上传到服务器 - 希望有人知道发生了什么。如果没有,我可以稍后尝试上传照片。
so, I save my picture to the sd card using fileOutputStream- i have the image formatted for jpeg- but the image saves distorted. this is weird because the thumbnail image is the correct representation of the image, but when opened, it's a series of horizontal lines.
here are some snippets from my code:
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
if(previewing){
camera.stopPreview();
previewing=false;
}
if(camera!=null){
try{
camera.setPreviewDisplay(surfaceHolder);
Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setPreviewSize(width, height);
camera.setParameters(parameters);
camera.startPreview();
previewing=true;
} catch(IOException e){}
}
}
and
@Override
public void onPictureTaken(byte[] data, Camera camera) {
OutputStream outputStream;
try{
outputStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis())); //to sd card
imageFileOS.write(data);
imageFileOS.flush();
imageFileOS.close();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
};
the images (of the horizontally distorted jpeg and the put-together thumbnail) aren't uploading to the server at the moment - hoping someone has an idea of what is going on. if not i can try to upload the pics later.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题已解决:
在 2.2 中,我在 2.3 中没有遇到问题
,图像保存时变形 - 这是通过我称为“getBestPictureSize”的方法修复的,该方法与我用来获取屏幕最佳预览尺寸的方法基本相同纵横比
这是为应用程序运行的设备获取最佳图片尺寸的方法...
这是 surfaceChanged 回调,其中利用上述方法来正确调整由camera.takePicture( ... )生成的图片的大小;
您还可以使用此图片大小调整方法来获取预览大小,方法是切换以下行:
希望
这对某人有帮助,因为我花了大约 10 个小时强调此功能在我的 2.3.4 上不起作用,但在我的 2.2 上起作用
话虽这么说,在 2.2 上运行 getBestPictureSize 会导致强制关闭。还没想好我要如何处理这个问题
problem solved:
in 2.2 i wasn't having an issue
in 2.3, the image was saving distorted- this was fixed with a method i called "getBestPictureSize" which is basically identical to the method i used to get the best preview size for the screen aspect ratio
this is the method for getting best picture size for the device the app is running on...
this is the surfaceChanged callback, in which the above method is utilized to properly size the picture resulting from camera.takePicture( ... );
you can also use this picture sizing method to get preview size, by switching out the line:
for
hope this helps someone, as i spent a like 10 hours stressing over this function wouldn't work on my 2.3.4 but would on my 2.2
that being said- running the getBestPictureSize on 2.2 causes a force close. haven't figured out how i'm going to handle that yet