2013-11-28 8 views
6

मैं संपीड़न विधि का उपयोग कर बिटमैप आकार को कम करने की कोशिश कर रहा हूं।बिटमैप संपीड़न बिटमैप बाइट आकार बदल नहीं रहा

यह मेरा कोड है:

public Bitmap compressImage(Bitmap image) { 
     Bitmap immagex = image; 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

     Log.i("before compress", immagex.getByteCount()+""); 

     boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos); 

     if(compress) 
      Log.i("after compress", immagex.getByteCount()+""); 
     else 
      Log.i("bad compress", "bad compress"); 

     return immagex; 
    } 

जब मैं अपने लॉग मैं प्राप्त की जाँच करें:

11-28 11:10:38.252: I/before compress(2429): 374544 
11-28 11:10:38.262: I/after compress(2429): 374544 

संपीड़ित क्यों काम नहीं करता है?

अद्यतन:

मैं इस कोड की कोशिश की:

public Bitmap compressImage(Bitmap image) { 
     Bitmap immagex = image; 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

     Log.i("before compress", immagex.getByteCount()+""); 

     boolean compress = immagex.compress(Bitmap.CompressFormat.JPEG, 10, baos); 

     Log.i("after compress 2", decodeSampledBitmapFromByte(image.getWidth(), image.getHeight(), baos.toByteArray()).getByteCount()+""); 

     return immagex; 
    } 

फिर भी एक ही बाइट गिनती

11-28 11:33:04.335: I/before compress(3472): 374544 
11-28 11:33:04.395: I/after compress 2(3472): 374544 
+0

कृपया लिंक follwing उल्लेख http://stackoverflow.com/questions/8417034/how-to-make-bitmap -compress-without-change-the-bitmap-size – prakash

+1

यह मेरा मुद्दा नहीं है। मैं अपने बिटमैप को स्केल करने के लिए बस स्केल करने के लिए स्मृति में आकार को कम करने के लिए स्केल करना नहीं चाहता हूं। – dasdasd

+2

इस समस्या के लिए एक समाधान मिला था? मुझे यहां एक ही मुद्दे का सामना करना पड़ रहा है। – Prateek

उत्तर

1

Bitmap.compress() तुम सिर्फ संपीड़न एल्गोरिथ्म और द्वारा निर्दिष्ट का उपयोग करना रास्ता संपीड़न ऑपरेशन समय की बजाय बड़ी मात्रा में लेता है। यदि आपको अपनी छवि के लिए स्मृति आवंटन को कम करने के लिए आकारों के साथ खेलने की आवश्यकता है, तो आपको बिटमैप.ऑप्शन का उपयोग करके अपनी छवि के स्केलिंग का उपयोग करने की आवश्यकता है, पहले बिटमैप सीमाओं की गणना करना और फिर इसे अपने निर्दिष्ट आकार में डीकोड करना होगा।

स्टैक ओवरव्लो पर मैंने पाया सबसे अच्छा नमूना यह है। Strange out of memory issue while loading an image to a Bitmap object

+2

मैं गुणवत्ता को कम करने के लिए बस बिटमैप को स्केल नहीं करना चाहता हूं। – dasdasd

4

के बाद, बिटमैप आकार को कम करने और यह बेस 64 में बदलने के लिए कोड है

Bitmap newBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, true); 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); 
    byte[] byteArray = baos.toByteArray(); 
    String imageEncoded = Base64.encodeToString(byteArray, Base64.DEFAULT); 

    Log.e("Base 64 String", imageEncoded); 
+0

टिप्पणी के लिए धन्यवाद लेकिन मैंने पहले से ही अन्य समाधान की कोशिश की है जो बदलते आकार का उपयोग न करें ... – dasdasd