使用Scalr调整图像大小,输出图像不正确,灰色

时间:2012-07-24 09:11:49

标签: java image resize imgscalr

  

可能重复:
  Resize an image in Java - Any Open Source Library?

我希望我能在录制之前调整图片大小!我试了好几次但没有成功。你能告诉我这个程序吗?我想使用imgscarlr或更好。谢谢

private static void getImages(String src,String Name) throws IOException {

    String folder = null;

    //Exctract the name of the image from the src attribute
    int indexname = src.lastIndexOf("/");


    if (indexname == src.length()) {
        src = src.substring(1, indexname);
    }

    indexname = src.lastIndexOf("/");
    String name = src.substring(indexname, src.length());

    System.out.println(name);

    //Open a URL Stream
    URL url = new URL(src);
    InputStream in = url.openStream();

    OutputStream out = new BufferedOutputStream(new FileOutputStream( folderPath+ name));

    for (int b; (b = in.read()) != -1;) {

        out.write(b);
    }


  BufferedImage originalImage = ImageIO.read(new File(folderPath+name));
    BufferedImage scaledImage = Scalr.resize(originalImage, 200);
    ImageIO.write(scaledImage, "jpg", new File(folderPath+name));
    }
    }

我没关系,但根据图片,我对灰色部分采样有问题!!!

2 个答案:

答案 0 :(得分:2)

您需要首先“加载”图片,查看ImageIO API以获取更多详细信息,之后根据您的需要,缩放图片相对简单

<强>更新

Reading image from URL

Image scaling API

Multi-step, hi-quality scaling

答案 1 :(得分:0)

我忘记了out.close();现在的工作