Reduzieren Sie die Größe einer bitmap auf eine angegebene Größe in Android

Möchte ich zum reduzieren der Größe der bitmap auf 200 Kb genau. Ich bekomme ein Bild von der sdcard, komprimieren und speichern Sie Sie auf die SD-Karte wieder mit einem anderen Namen in einem anderen Verzeichnis. Die Komprimierung funktioniert gut (3 mb wie das Bild komprimiert auf rund 100 kb). Ich schrieb die folgenden Zeilen des codes für dieses:

String imagefile ="/sdcard/DCIM/100ANDRO/DSC_0530.jpg";
Bitmap bm = ShrinkBitmap(imagefile, 300, 300);

//this method compresses the image and saves into a location in sdcard
    Bitmap ShrinkBitmap(String file, int width, int height){

         BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

            if (heightRatio > 1 || widthRatio > 1)
            {
             if (heightRatio > widthRatio)
             {
              bmpFactoryOptions.inSampleSize = heightRatio;
             } else {
              bmpFactoryOptions.inSampleSize = widthRatio; 
             }
            }

            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            ByteArrayOutputStream stream = new ByteArrayOutputStream();   
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);   
            byte[] imageInByte = stream.toByteArray(); 
            //this gives the size of the compressed image in kb
            long lengthbmp = imageInByte.length / 1024; 

            try {
                bitmap.compress(CompressFormat.JPEG, 100, new FileOutputStream("/sdcard/mediaAppPhotos/compressed_new.jpg"));
            } catch (FileNotFoundException e) {
                //TODO Auto-generated catch block
                e.printStackTrace();
            }


         return bitmap;
        }
  • ändern Sie Breite und Höhe des Bildes..
  • du meinst, um das Bild 200x200?
  • ja, als pro Sie wollen 200kb.. versuchen, bis Sie Ihr Ergebnis..
  • ok, wenn ich eine Konstante Breite und eine Höhe, werden die Bilder in der gleichen Größe immer?, Ich habe das schon getan: ShrinkBitmap(imagefile, 300, 300)
  • Nein, ändern Sie die Breite und Höhe entsprechend Ihrer Größe.. wenn Sie bekommen 100 Kb auf 300*300, dann für Größe 200 Kb, erhöhen sowohl die Breite und Höhe..
  • 200 x 200 = 81kb 300 x 300 = 81kb 350 x 350 = 81kb 400 x 300 = 310kb 400 x 400 = 310kb Oben sind die Werte, die ich bekomme, scheint nicht zu schlagen, 200 Kb
  • lassen Sie uns weiter, diese Diskussion im chat
  • siehe die Antwort, ich habe es beantwortet.

Schreibe einen Kommentar