2013-04-17 16 views
16

Voglio che il mio codice ridimensioni l'immagine prima di salvarla ma non riesco a trovare nulla su di esso su Google. Potresti aiutarmi per favore?Android scatta foto e ridimensiona prima di salvare su scheda SD

Questo è il codice (da Android doc):

private void galleryAddPic() { 
    Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE"); 
    File f = new File(mCurrentPhotoPath); 

    picturePathForUpload = mCurrentPhotoPath; 

    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    this.sendBroadcast(mediaScanIntent); 
} 

Dopo di che, devo caricarlo su un server.

Grazie mille.

+0

Hi Florian Hai dato un'occhiata a questa pagina: http://stackoverflow.com/questions/12780375/resize-image-after-capture-it-from-native-camera-but-before-save-it-to-sd -card Bonne journée! :) –

risposta

26

È possibile salvare l'immagine bitmap seguente codice

Bitmap photo = (Bitmap) "your Bitmap image"; 
photo = Bitmap.createScaledBitmap(photo, 100, 100, false); 
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 

File f = new File(Environment.getExternalStorageDirectory() 
     + File.separator + "Imagename.jpg"); 
f.createNewFile(); 
FileOutputStream fo = new FileOutputStream(f); 
fo.write(bytes.toByteArray()); 
fo.close(); 
+7

questo comporterà un'immagine in miniatura e non uno a grandezza naturale. –

+1

anche questo influenzerà la chiarezza dell'immagine –

+0

Considerati i commenti sopra, si prega di dare un'occhiata alla mia risposta: http://stackoverflow.com/a/36210688/4038790 Penso che sia meglio risolvere il tuo problema. – lwdthe1

4

prima convertire l'immagine bitmap quindi utilizzare questo codice:

Bitmap yourBitmap; 
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true); 
+0

Grazie a @John! Ma ... come posso salvarlo? –

+0

Controlla questo link, è molto simile a quello che vuoi: http://stackoverflow.com/questions/7687723/update-android-image-gallery-with-newly-created-bitmap – John

1

See this sarà aiuto pieno. In generale, se il tuo sta assumendo un'immagine dalla telecamera utilizzando intento si otterrà il uri dell'immagine come risultato lo si legge immagine in scala verso il basso, come e conservarla nello stesso luogo

0

Se si desidera acquisire un'immagine a dimensione intera, non si ha altra scelta che salvarla sul carrello sd e quindi modificare la dimensione dell'immagine.

Se tuttavia l'immagine di anteprima è sufficiente, non è necessario salvarla sulla scheda SD ed è possibile estrarla dagli extra dell'intento restituito.

Potete guardare questa guida che ho scritto per entrambi i metodi di prendere le immagini utilizzando la configurazione in fotocamera Activity:

Guide: Android: Use Camera Activity for Thumbnail and Full Size Image

4

Dopo aver letto le altre risposte e non trovare esattamente quello che volevo, ecco il mio approccio ad acquisire una bitmap opportunamente ridimensionata. Questo è un adattamento della risposta di Prabu.

rende sicuro il quadro è scalato in un modo che non deforma le dimensioni della foto:

public saveScaledPhotoToFile() { 
    //Convert your photo to a bitmap 
    Bitmap photoBm = (Bitmap) "your Bitmap image"; 
    //get its orginal dimensions 
    int bmOriginalWidth = photoBm.getWidth(); 
    int bmOriginalHeight = photoBm.getHeight(); 
    double originalWidthToHeightRatio = 1.0 * bmOriginalWidth/bmOriginalHeight; 
    double originalHeightToWidthRatio = 1.0 * bmOriginalHeight/bmOriginalWidth; 
    //choose a maximum height 
    int maxHeight = 1024; 
    //choose a max width 
    int maxWidth = 1024; 
    //call the method to get the scaled bitmap 
    photoBm = getScaledBitmap(photoBm, bmOriginalWidth, bmOriginalHeight, 
      originalWidthToHeightRatio, originalHeightToWidthRatio, 
      maxHeight, maxWidth); 

    /**********THE REST OF THIS IS FROM Prabu's answer*******/ 
    //create a byte array output stream to hold the photo's bytes 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    //compress the photo's bytes into the byte array output stream 
    photoBm.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 

    //construct a File object to save the scaled file to 
    File f = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "Imagename.jpg"); 
    //create the file 
    f.createNewFile(); 

    //create an FileOutputStream on the created file 
    FileOutputStream fo = new FileOutputStream(f); 
    //write the photo's bytes to the file 
    fo.write(bytes.toByteArray()); 

    //finish by closing the FileOutputStream 
    fo.close(); 
} 

private static Bitmap getScaledBitmap(Bitmap bm, int bmOriginalWidth, int bmOriginalHeight, double originalWidthToHeightRatio, double originalHeightToWidthRatio, int maxHeight, int maxWidth) { 
    if(bmOriginalWidth > maxWidth || bmOriginalHeight > maxHeight) { 
     Log.v(TAG, format("RESIZING bitmap FROM %sx%s ", bmOriginalWidth, bmOriginalHeight)); 

     if(bmOriginalWidth > bmOriginalHeight) { 
      bm = scaleDeminsFromWidth(bm, maxWidth, bmOriginalHeight, originalHeightToWidthRatio); 
     } else { 
      bm = scaleDeminsFromHeight(bm, maxHeight, bmOriginalHeight, originalWidthToHeightRatio); 
     } 

     Log.v(TAG, format("RESIZED bitmap TO %sx%s ", bm.getWidth(), bm.getHeight())); 
    } 
    return bm; 
} 

private static Bitmap scaleDeminsFromHeight(Bitmap bm, int maxHeight, int bmOriginalHeight, double originalWidthToHeightRatio) { 
    int newHeight = (int) Math.min(maxHeight, bmOriginalHeight * .55); 
    int newWidth = (int) (newHeight * originalWidthToHeightRatio); 
    bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true); 
    return bm; 
} 

private static Bitmap scaleDeminsFromWidth(Bitmap bm, int maxWidth, int bmOriginalWidth, double originalHeightToWidthRatio) { 
    //scale the width 
    int newWidth = (int) Math.min(maxWidth, bmOriginalWidth * .75); 
    int newHeight = (int) (newWidth * originalHeightToWidthRatio); 
    bm = Bitmap.createScaledBitmap(bm, newWidth, newHeight, true); 
    return bm; 
} 

Ecco un corrispondente link al mio GitHub Gist: https://gist.github.com/Lwdthe1/2d1cd0a12f30c18db698

+1

Il metodo getScaledBitmap (...) eviterà il ridimensionamento se la foto è quadrata (larghezza == altezza).devi eliminare questa riga: START OF LINE: else if (bmOriginalHeight> bmOriginalWidth) {END OF LINE e usa semplicemente else { –

+1

Non dovresti usare Math.min? – lionheart

+0

@UdiReshef buona cattura. Anche tu cuore di leone. Risolto – lwdthe1

0
BitmapFactory.Options optionsSignature = new BitmapFactory.Options(); 
final Bitmap bitmapSignature = BitmapFactory.decodeFile(
fileUriSignature.getPath(), optionsSignature); 
Bitmap resizedSignature = Bitmap.createScaledBitmap(
       bitmapSignature, 256, 128, true); 
signature.setImageBitmap(resizedSignature); 
Problemi correlati