2017-06-17 108 views
0

這裏是我創建的文件的格式如何上傳JPEG圖像而不丟失EXIF?

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; 
    } 

這裏保存我的字符串將其發送到我的web服務(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; 
    } 

的主要問題是,我失去EXIF。 另一種方式,但太慢將其發送到Web服務:

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); 

我使用改裝發送information.The服務具有接收的文件(encodedPrueba)的字符串。

+0

我忘了我想在發送之前壓縮文件。 –

+0

按照這個... http://blog.aimanbaharum.com/2016/03/26/android-image-multipart-upload/ –

+0

我需要發送它作爲字符串之前發送它,這可能嗎? –

回答

0

主要問題是我輸了EXIF。

嗯,是的。 JPEG文件可能有EXIF信息。 A Bitmap沒有。默認情況下,從Bitmap創建的JPEG不會。您需要從原始JPEG中讀取EXIF信息,並將相同的信息(如果需要進行調整)添加到新的JPEG中。

+0

但是這個新的JPEG不會有相同的大小。 –

+0

@JeanCarloFloresCarrasco:正如我寫的,您需要從原始JPEG中讀取EXIF信息,並將相同的信息(**或需要調整**)添加到新的JPEG中。因此,對於任何需要根據JPEG更改而需要更改的EXIF信息,您需要計算新值或跳過這些EXIF標籤。 – CommonsWare

相關問題