簡體   English   中英

如何縮小圖片尺寸?

[英]How to reduce picture size?

我想將相機拍攝的照片發送到服務器。 但是它的大小大約為1M,我相信是兩個。 因此,我想減小照片的尺寸。 下面是我正在使用的方法。
saveFile只需將位圖保存到文件並返回其路徑。
calculateInSampleSize返回inSampleSize調整照片dementions到選擇的。
decodeSampledBitmapFromResource從文件中解碼birmap,並使用新參數對其進行重建。
Evrything很好,但似乎不起作用。 small3.png的尺寸為1240 * 768 * 24,尺寸仍為〜1M。

我用

photo = saveFile(decodeSampledBitmapFromResource(picturePath,
                    800, 800),3);

方法

public String saveFile(Bitmap bm,int id) {
        String file_path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/Mk";
        File dir = new File(file_path);
        if (!dir.exists())
            dir.mkdirs();
        File file = new File(dir, "smaller" + id + ".png");
        FileOutputStream fOut;
        try {
            fOut = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
            fOut.flush();
            fOut.close();


            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


        return file.getPath();

    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            // Calculate ratios of height and width to requested height and
            // width
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will
            // guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }

        return inSampleSize;
    }
public static Bitmap decodeSampledBitmapFromResource(String path,
            int reqWidth, int reqHeight) {
        Log.d("path", path);
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }

為什么要兩次寫入圖像,一次只能完成一次

我所做的

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;

Bitmap myBitmap = BitmapFactory.decodeFile({ImagePath}, options);
FileOutputStream fileOutputStream = new FileOutputStream({ImagePath});
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
myBitmapClose.compress(CompressFormat.JPEG, imageQuality, bos);
if (bos != null) {
    bos.flush();
    bos.close();
}
if (fileOutputStream != null) {
    fileOutputStream.flush();
    fileOutputStream.close();
}

一定會壓縮線路

 myBitmapClose.compress(CompressFormat.JPEG, imageQuality, bos);

在哪里設置imageCompression類型CompressFormat.JPEG

多數民眾贊成在我的代碼...我想減少路徑為字符串的圖像的大小。

String pics=(arraylist.get(position).getImage()); 
try{ 
   BitmapFactory.Options op = new BitmapFactory.Options(); 
   op.inJustDecodeBounds = true; 
   //op.inSampleSize = 20; op.outWidth = 25; 
   op.outHeight = 35; 
   bmp = BitmapFactory.decodeFile(pics); 
   image.setImageBitmap(bmp); 
 }catch(Exception e) {
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM