调整android中的图像大小

时间:2014-01-07 05:57:09

标签: java android resize

我有一些代码可以从文件夹中获取图像,当切换按钮打开时,图像将被调整大小并存储到另一个文件夹。

当我使用切换按钮运行此代码时,应用程序崩溃说“不幸的是MyApps已停止”。这段代码是用Java编写的,我想在android中运行这个程序。我怎样才能做到这一点?

public class ImageTest 
    {
        private static int height;
        private static int width ;
        private static int IMG_HEIGHT=480;
        private static int IMG_WIDTH;

        public int image()
    {


        try{
            BufferedImage bimg = ImageIO.read(new File("D:\\image\\12.jpg"));
             width          = bimg.getWidth();
             height         = bimg.getHeight();
            System.out.println("Width:"+width+" and height:"+height);


            IMG_WIDTH = ((IMG_HEIGHT * width)/height);

            BufferedImage originalImage = ImageIO.read(new File("d:\\image\\12.jpg"));
            int type = originalImage.getType() == 0? BufferedImage.TYPE_INT_ARGB : originalImage.getType();

            BufferedImage resizeImageJpg = resizeImage(originalImage, type);
            ImageIO.write(resizeImageJpg, "jpg", new File("d:\\image\\12_jpg.jpg")); 

            BufferedImage resizeImagePng = resizeImage(originalImage, type);
            ImageIO.write(resizeImagePng, "png", new File("d:\\image\\12_png.png")); 

            BufferedImage resizeImageHintJpg = resizeImageWithHint(originalImage, type);
            ImageIO.write(resizeImageHintJpg, "jpg", new File("d:\\image\\12_hint_jpg.jpg")); 

            BufferedImage resizeImageHintPng = resizeImageWithHint(originalImage, type);
            ImageIO.write(resizeImageHintPng, "png", new File("d:\\image\\12_hint_png.png"));
            return 1;

        }catch(Exception e){
            System.out.println(e.getMessage());
            return 0;
        }

    }

        private static  BufferedImage resizeImage(BufferedImage originalImage, int type)
        {
        BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
        g.dispose();

        return resizedImage;
        }

        private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type)
        {

        BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
        g.dispose();    
        g.setComposite(AlphaComposite.Src);

        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING,
        RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);

        return resizedImage;
        }   
    }

1 个答案:

答案 0 :(得分:0)

好的,你可以尝试这个Stackoverflow Link;与你提出的问题类似的问题。关于你的代码,你不能直接在Android中运行java代码。

首先参考上面的链接,然后尝试运行。