2012-03-07 21 views
9

से पहले चित्र को घुमाएं मैंने अभी अपनी कैमरा गतिविधि समाप्त की है और यह आश्चर्यजनक रूप से डेटा को सहेज रहा है। मैं के बाद चित्र लिया जाता है तो क्या करें:एंड्रॉइड घुमाएं चित्र

protected void savePictureData() { 
    try { 
     FileOutputStream fs = new FileOutputStream(this.photo); 
     fs.write(this.lastCamData); 
     fs.close(); //okay, wonderful! file is just written to the sdcard 

     //--------------------- 
     //--------------------- 
     //TODO in here: dont save just the file but ROTATE the image and then save it! 
     //--------------------- 
     //--------------------- 


     Intent data = new Intent(); //just a simple intent returning some data... 
     data.putExtra("picture_name", this.fname); 
     data.putExtra("byte_data", this.lastCamData); 
     this.setResult(SAVED_TOOK_PICTURE, data); 
     this.finish(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     this.IOError(); 
    } 

} 

क्या मैं करना चाहते हैं इसके बाद के संस्करण कोड में दी गई टिप्पणी के रूप में पहले से ही है। मैं नहीं चाहता कि छवि सिर्फ फ़ाइल में सहेजी जाए लेकिन घुमाए और फिर बचाया जाए! धन्यवाद!

// संपादित: क्या मैं करने के लिए वर्तमान में कर रहा हूँ

byte[] pictureBytes; 
Bitmap thePicture = BitmapFactory.decodeByteArray(this.lastCamData, 0, this.lastCamData.length); 
Matrix m = new Matrix(); 
m.postRotate(90); 
thePicture = Bitmap.createBitmap(thePicture, 0, 0, thePicture.getWidth(), thePicture.getHeight(), m, true); 

ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
thePicture.compress(CompressFormat.JPEG, 100, bos); 
pictureBytes = bos.toByteArray(); 

FileOutputStream fs = new FileOutputStream(this.photo); 
fs.write(pictureBytes); 
fs.close(); 
Intent data = new Intent(); 
data.putExtra("picture_name", this.fname); 
data.putExtra("byte_data", pictureBytes); 
this.setResult(SAVED_TOOK_PICTURE, data); 
this.finish(); 
+0

के संभावित डुप्लिकेट [एंड्रॉयड: कैसे एक केंद्र बिंदु पर एक बिटमैप को घुमाने के लिए] (http://stackoverflow.com/questions/4166917/android-how-to-rotate-a-bitmap-on-a -center-point) – slayton

+0

नहीं, वास्तव में, – androidavid

+0

ठीक से घुमाने और सहेजने के तरीके के बारे में और अधिक जानकारी मेरा समाधान काम करता है - प्रोजेक्ट को साफ़ किया गया है ... – androidavid

उत्तर

4

(वर्क्स, लेकिन अभी भी बड़ी छवियों के साथ स्मृति मुद्दों में चलता है) इससे पहले कि आप अपने FileOutputStream आप एक नया Bitmap मूल से बना सकते हैं Matrix का उपयोग करके बदल दिया गया है।

createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) 

कहाँ m एक मैट्रिक्स है कि अपने मूल बिटमैप स्थानांतरित होगा परिभाषित करता है: है कि आप इस विधि का प्रयोग करेंगे ऐसा करने के लिए।

कैसे इस सवाल पर यह देखो करने के लिए पर एक उदाहरण के लिए: Android: How to rotate a bitmap on a center point

+0

हे, मेरे संपादन पर एक नज़र डालें। मैंने कोशिश की लेकिन यह इस तरह काम नहीं करता है ...:/ – androidavid

+0

मैं ऐसा कुछ करने की कोशिश कर रहा हूं। लेकिन हर बार जब मैं बिटमैप की प्रतिलिपि करता हूं तो मुझे अपवाद मिला: स्मृति से बाहर ... –

+0

@ जौबर्टवस्कोनसेलोस, आपको तब छवि को स्केल करने की आवश्यकता है। – slayton

17

तुम बस एसडी कार्ड से पथ पढ़ सकते हैं और निम्नलिखित कोड ... यह वह घूमते के बाद मौजूदा तस्वीर बदलें करेंगे ..

नोट: Exif अधिकांश उपकरणों पर काम नहीं करता है, यह गलत डेटा देता है, इसलिए किसी भी डिग्री को सहेजने से पहले घुमावदार हार्ड कोड के लिए अच्छा है, आपको बस पोस्टरोट में कोण मान बदलना होगा आप चाहते हैं कि कोई भी।

String photopath = tempphoto.getPath().toString(); 
    Bitmap bmp = BitmapFactory.decodeFile(photopath); 

    Matrix matrix = new Matrix(); 
    matrix.postRotate(90); 
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 

    FileOutputStream fOut; 
    try { 
     fOut = new FileOutputStream(tempphoto); 
     bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut); 
     fOut.flush(); 
     fOut.close(); 

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

} 
+0

यह मेरे लिए काम किया। धन्यवाद। – BRS

+0

ग्रेट बॉस, मेरे लिए भी काम किया! –

+0

समस्या है कि हमारे पास फोटो के साथ प्रोग्रामर हैं बिटमैप कक्षा से आता है! हमेशा, आप बिटमैप क्लास के लिए छवि पास करते हैं, यह फोटो को 90 डिग्री तक घुमाता है। फिर समाधान यह है कि हम हमेशा बिटमैप वर्ग का उपयोग करते हैं और हमें डी फोटो दिखाने की ज़रूरत है जिसे हमें 'matrix.postRotate (90)' लागू करने की आवश्यकता है; घुमावदार बिटमैप बनाने के लिए। –

संबंधित मुद्दे