2010-07-14 16 views
5

मुझे बिटमैप मिला है ... और यदि बिटमैप की ऊंचाई maxHeight से अधिक है, या चौड़ाई maxWidth से अधिक है, तो मैं आनुपातिक रूप से छवि का आकार बदलना चाहता हूं ताकि यह maxWidth X में फिट हो अधिकतम ऊँचाई। यहाँ मैं क्या कोशिश कर रहा हूँ है:आनुपातिक रूप से बिटमैप का आकार बदलें

BitmapDrawable bmp = new BitmapDrawable(getResources(), PHOTO_PATH); 

    int width = bmp.getIntrinsicWidth(); 
    int height = bmp.getIntrinsicHeight(); 

    float ratio = (float)width/(float)height; 

    float scaleWidth = width; 
    float scaleHeight = height; 

    if((float)mMaxWidth/(float)mMaxHeight > ratio) { 
     scaleWidth = (float)mMaxHeight * ratio; 
    } 
    else { 
     scaleHeight = (float)mMaxWidth/ratio; 
    } 

    Matrix matrix = new Matrix(); 
    matrix.postScale(scaleWidth, scaleHeight); 

    Bitmap out = Bitmap.createBitmap(bmp.getBitmap(), 
      0, 0, width, height, matrix, true); 

    try { 
     out.compress(Bitmap.CompressFormat.JPEG, 100, 
       new FileOutputStream(PHOTO_PATH)); 
    } 
    catch(FileNotFoundException fnfe) { 
     fnfe.printStackTrace(); 
    } 

मैं निम्न अपवाद प्राप्त करें:

java.lang.IllegalArgumentException: bitmap size exceeds 32bits

क्या मैं गलत यहाँ कर रहा हूँ?

+0

अतीत आप कर सकते हैं को सही कोड यहाँ? मुझे एक ही अपवाद मिल रहा है – Mahesh

उत्तर

8

आपका स्केलविड्थ और स्केल हाइट स्केल कारक होना चाहिए (इसलिए बहुत बड़ी संख्या नहीं) लेकिन आपका कोड वास्तविक चौड़ाई और ऊंचाई में गुजरता प्रतीत होता है जिसे आप ढूंढ रहे हैं। तो आप अपने बिटमैप के आकार को बड़े पैमाने पर बढ़ा रहे हैं।

मुझे लगता है कि कोड के साथ स्केलविड्थ और स्केल हाइट को प्राप्त करने के लिए अन्य समस्याएं हैं। एक बात के लिए आपके कोड में हमेशा स्केलविड्थ = चौड़ाई या स्केलहेइट = ऊंचाई है, और उनमें से केवल एक को बदलता है, इसलिए आप अपनी छवि के पहलू अनुपात को विकृत करने जा रहे हैं। यदि आप बस छवि का आकार बदलना चाहते हैं तो आपके पास केवल एक स्केल फैक्टर होना चाहिए।

इसके अलावा, आपका स्टेटमेंट क्यों प्रभावी रूप से maxRatio> अनुपात क्यों जांचता है? क्या आपको चौड़ाई> maxWidth या ऊंचाई> maxHeight की जांच नहीं करनी चाहिए?

1

इसका कारण यह है scaleWidth या scaleHeight का मूल्य बहुत बड़ा है, scaleWidth या scaleHeight मतलब बड़ा या bitmap आकार करने के लिए की दर, लेकिन नहीं width या height, बहुत बड़ी दर नेतृत्व हटना है 32 बिट

matrix.postScale(scaleWidth, scaleHeight); 
से अधिक है
1

यह है मैं कैसे यह किया:

public Bitmap decodeAbtoBm(byte[] b){ 
    Bitmap bm; // prepare object to return 

    // clear system and runtime of rubbish 
    System.gc(); 
    Runtime.getRuntime().gc(); 

    //Decode image size only 
    BitmapFactory.Options oo = new BitmapFactory.Options(); 
    // only decodes size, not the whole image 
    // See Android documentation for more info. 
    oo.inJustDecodeBounds = true; 
    BitmapFactory.decodeByteArray(b, 0, b.length ,oo); 

    //The new size we want to scale to 
    final int REQUIRED_SIZE=200; 

    // Important function to resize proportionally. 
    //Find the correct scale value. It should be the power of 2. 
    int scale=1; 
    while(oo.outWidth/scale/2>=REQUIRED_SIZE 
      && oo.outHeight/scale/2>=REQUIRED_SIZE) 
      scale*=2; // Actual scaler 

    //Decode Options: byte array image with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    o2.inSampleSize=scale; // set scaler 
    o2.inPurgeable = true; // for effeciency 
    o2.inInputShareable = true; 

    // Do actual decoding, this takes up resources and could crash 
    // your app if you do not do it properly 
    bm = BitmapFactory.decodeByteArray(b, 0, b.length,o2); 

    // Just to be safe, clear system and runtime of rubbish again! 
    System.gc(); 
    Runtime.getRuntime().gc(); 

    return bm; // return Bitmap to the method that called it 
} 
संबंधित मुद्दे