2017-06-17 1 views
0

Hier ist, wo ich das Format der Datei erstellenWie kann ich ein JPEG-Bild hochladen, ohne EXIF ​​zu verlieren?

private File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new 
    Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_PICTURES); 

    File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = image.getAbsolutePath(); 
    Log.e("Getpath", "Cool" + mCurrentPhotoPath); 
    return image; 
    } 

Hier habe ich meine String speichern Service auf meiner Web zu senden (REST) ​​

private String setPic(ImageView v) throws IOException { 
    // Get the dimensions of the View 
    targetW = 320; 
    targetH = 250; 

    // Get the dimensions of the bitmap 
    BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
    bmOptions.inJustDecodeBounds = true; 
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
    int photoW = bmOptions.outWidth; 
    int photoH = bmOptions.outHeight; 

    // Determine how much to scale down the image 
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH); 

    // Decode the image file into a Bitmap sized to fill the View 
    bmOptions.inJustDecodeBounds = false; 
    bmOptions.inSampleSize = scaleFactor; 
    bmOptions.inPurgeable = true; 

    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 

    v.setImageBitmap(bitmap); 
    v.setVisibility(View.VISIBLE); 

    if (PICTURE_DNI){ 
     timeStapDNI = new Date().getTime(); 
    }else{ 
     timeStapSign =new Date().getTime(); 
    } 

    File filePhoto = new File(mCurrentPhotoPath); 

    FileInputStream fis = new FileInputStream(filePhoto); 
    Bitmap bi = BitmapFactory.decodeStream(fis); // EXIF info lost 
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    bi.compress(Bitmap.CompressFormat.JPEG, 50, baos); 
    byte[] data = baos.toByteArray(); 
    String encodedPrueba = Base64.encodeToString(data,Base64.DEFAULT); 
    Log.i("Data Input ", "" + encodedPrueba); 
    v.setImageBitmap(bitmap); 

    return encodedPrueba; 
    } 

Das Hauptproblem ist, ich EXIF ​​bin zu verlieren. Eine andere Möglichkeit, aber zu langsam ist es um Web-Service zu senden:

File filePhoto = new File(mCurrentPhotoPath); 
    byte[] fileData = new byte[(int) filePhoto.length()]; 
    DataInputStream dis = new DataInputStream(new 
    FileInputStream(filePhoto)); 
    dis.readFully(fileData); 
    dis.close(); 
    String encodedPrueba = Base64.encodeToString(fileData,Base64.DEFAULT); 

ich Retrofit bin mit dem Informationen.Verfahren Dienst senden eine Zeichenfolge für die Datei (encodedPrueba) empfangen.

+0

Ich habe vergessen, dass ich die Datei vor dem Senden komprimieren möchte. –

+0

folgen Sie diesem ... http: //blog.aimanbaharum.com/2016/03/26/android-image-multi-part-upload/ –

+0

Ich muss es als String senden, um es zu senden, ist das möglich? –

Antwort

0

Das Hauptproblem ist, ich verliere EXIF.

Nun, ja. Eine JPEG-Datei kann EXIF-Informationen enthalten. A Bitmap nicht. Ein JPEG, das mit Bitmap erstellt wurde, ist standardmäßig nicht verfügbar. Sie müssten die EXIF-Informationen aus dem Original-JPEG lesen und die gleichen Informationen zu Ihrem neuen JPEG hinzufügen (oder bei Bedarf anpassen).

+0

Aber dieses neue JPEG würde nicht die gleiche Größe haben. –

+0

@JeanCarloFloresCarrasco: Wie ich geschrieben habe, müssten Sie die EXIF-Informationen aus dem Original-JPEG lesen und die gleichen Informationen (** oder angepasst, falls erforderlich **) zu Ihrem neuen JPEG hinzufügen. Für alle EXIF-Informationen, die aufgrund Ihrer Änderungen an JPEG geändert werden müssen, müssen Sie entweder die neuen Werte berechnen oder diese EXIF-Tags überspringen. – CommonsWare

Verwandte Themen