图像调整大小代码错误

时间:2014-01-07 11:33:54

标签: android image bitmap resize

嘿大家这是我们可以调整图像大小的代码,但是在第1行和第2行有一个我无法解决的问题请帮帮我

Bitmap image2 = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(image2);
        /*String incident_ID = IncidentFormActivity.incident_id;*/

        String imagepath = "\1.jpg";
        File file = new File(imagepath);

                double xFactor = 0;
                double width = Double.valueOf(image2.getWidth());
                Log.v("WIDTH", String.valueOf(width));
                double height = Double.valueOf(image2.getHeight());
                Log.v("height", String.valueOf(height));



        Log.v("Nheight", String.valueOf(width*xFactor));
        Log.v("Nweight", String.valueOf(height*xFactor));
        int Nheight = 480;
        int NWidth = (int) ((Nheight * width)/height);

        Bitmap bm = Bitmap.createScaledBitmap( image2,NWidth, Nheight, true);
        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        bm.compress(CompressFormat.PNG, 100, ostream);
        ostream.close(); 

1 个答案:

答案 0 :(得分:0)

使用此功能。它对我来说很完美

static public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
        int width = image.getWidth();
        int height = image.getHeight();
        float bitmapRatio = width / height;
        if (bitmapRatio > 0) {
            width = maxSize;
            height = (int) (width / bitmapRatio);
        } else {
            height = maxSize;
            width = (int) (height * bitmapRatio);
        }
        return Bitmap.createScaledBitmap(image, width, height, true);
}

此处最大尺寸是指图像将缩小之前的像素

例如,如果要减少到640px 只需调用`

Bitmap image2 = (Bitmap) data.getExtras().get("data"); 
getResizedBitmap(image2 , 640);