sd_card保存后,jpeg保存失真

发布于 2024-12-20 09:27:28 字数 1388 浏览 3 评论 0原文

因此,我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晨曦÷微暖 2024-12-27 09:27:28

问题已解决:

在 2.2 中,我在 2.3 中没有遇到问题

,图像保存时变形 - 这是通过我称为“getBestPictureSize”的方法修复的,该方法与我用来获取屏幕最佳预览尺寸的方法基本相同纵横比

这是为应用程序运行的设备获取最佳图片尺寸的方法...

private Camera.Size getBestPicturSize(int width, int height)
{
        Camera.Size result=null;    
        Camera.Parameters p = camera.getParameters();
        for (Camera.Size size : p.getSupportedPictureSizes()) {
            if (size.width<=width && size.height<=height) {
                if (result==null) {
                    result=size;
                } else {
                    int resultArea=result.width*result.height;
                    int newArea=size.width*size.height;

                    if (newArea>resultArea) {
                        result=size;
                    }
                }
            }
        }
    return result;

}

这是 surfaceChanged 回调,其中利用上述方法来正确调整由camera.takePicture( ... )生成的图片的大小;

private Camera.Size getBestPicturSize(int width, int height)
{
        Camera.Size result=null;    
        Camera.Parameters p = camera.getParameters();
        for (Camera.Size size : p.getSupportedPictureSizes()) {
            if (size.width<=width && size.height<=height) {
                if (result==null) {
                    result=size;
                } else {
                    int resultArea=result.width*result.height;
                    int newArea=size.width*size.height;

                    if (newArea>resultArea) {
                        result=size;
                    }
                }
            }
        }
    return result;

}

您还可以使用此图片大小调整方法来获取预览大小,方法是切换以下行:

for (Camera.Size size : p.getSupportedPictureSizes()) {

希望

for (Camera.Size size : p.getSupportedPreviewSizes()) {

这对某人有帮助,因为我花了大约 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...

private Camera.Size getBestPicturSize(int width, int height)
{
        Camera.Size result=null;    
        Camera.Parameters p = camera.getParameters();
        for (Camera.Size size : p.getSupportedPictureSizes()) {
            if (size.width<=width && size.height<=height) {
                if (result==null) {
                    result=size;
                } else {
                    int resultArea=result.width*result.height;
                    int newArea=size.width*size.height;

                    if (newArea>resultArea) {
                        result=size;
                    }
                }
            }
        }
    return result;

}

this is the surfaceChanged callback, in which the above method is utilized to properly size the picture resulting from camera.takePicture( ... );

private Camera.Size getBestPicturSize(int width, int height)
{
        Camera.Size result=null;    
        Camera.Parameters p = camera.getParameters();
        for (Camera.Size size : p.getSupportedPictureSizes()) {
            if (size.width<=width && size.height<=height) {
                if (result==null) {
                    result=size;
                } else {
                    int resultArea=result.width*result.height;
                    int newArea=size.width*size.height;

                    if (newArea>resultArea) {
                        result=size;
                    }
                }
            }
        }
    return result;

}

you can also use this picture sizing method to get preview size, by switching out the line:

for (Camera.Size size : p.getSupportedPictureSizes()) {

for

for (Camera.Size size : p.getSupportedPreviewSizes()) {

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文