2013-06-06 2 views
7

में किसी निर्दिष्ट आकार में बिटमैप के आकार को कम करें, मैं बिटमैप के आकार को 200kb तक बिल्कुल कम करना चाहता हूं। मुझे एसडीकार्ड से एक छवि मिलती है, इसे संपीड़ित करें और इसे एक अलग नाम के साथ फिर से एक अलग नाम के साथ एसडीकार्ड में सहेजें। संपीड़न ठीक काम करता है (छवि की तरह 3 एमबी लगभग 100 केबी तक संपीड़ित है)। मैं इसके लिए कोड निम्नलिखित लाइनों ने लिखा है:एंड्रॉइड

/** 
    * reduces the size of the image 
    * @param image 
    * @param maxSize 
    * @return 
    */ 
    public Bitmap getResizedBitmap(Bitmap image, int maxSize) { 
     int width = image.getWidth(); 
     int height = image.getHeight(); 

     float bitmapRatio = (float)width/(float) height; 
     if (bitmapRatio > 1) { 
      width = maxSize; 
      height = (int) (width/bitmapRatio); 
     } else { 
      height = maxSize; 
      width = (int) (height * bitmapRatio); 
     } 
     return Bitmap.createScaledBitmap(image, width, height, true); 
    } 

विधि बुला:

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; 
     } 
+0

परिवर्तन उस छवि की चौड़ाई और ऊंचाई .. – Riser

+0

क्या आप छवि 200x200 बनाने का मतलब है? – TharakaNirmana

+0

हाँ, जैसा कि आप 200kb चाहते हैं .. अपना परिणाम प्राप्त करने तक प्रयास करें .. – Riser

उत्तर

16

मैं एक जवाब है कि मेरे लिए पूरी तरह से काम करता है पाया

Bitmap converetdImage = getResizedBitmap(photo, 500); 
  • तस्वीर है अपने बिटमैप
+0

यह फ़ंक्शन बिटमैप आकार को कम करेगा यदि यह बड़ा है, लेकिन छोटे बिटमैप्स के बारे में क्या है। क्या वे विनिर्देश के अनुसार बढ़ाए जाएंगे? – NarendraJi

+0

यह ब्लैक बिटमैप क्यों देता है? – Sermilion

+1

@TharakaNirmana maxSize क्या है? आपके विधि कॉलिंग में 500, 500 केबी या 500 एमबी है? – TheQ