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 和放在一起的缩略图)目前没有上传到服务器 - 希望有人知道发生了什么。如果没有,我可以稍后尝试上传照片。

4

1 回答 1

0

问题解决了:

在 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 会导致强制关闭。还没想好我要怎么处理

于 2011-12-11T22:21:00.527 回答