如何在不丢失EXIF的情况下上传JPEG图像?

时间:2017-06-17 14:33:24

标签: android jpeg retrofit2

这是我创建文件格式的地方

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

这里我保存我的String以将其发送到我的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);

我使用改造来发送信息。服务必须接收文件的字符串(encodedPrueba)。

1 个答案:

答案 0 :(得分:0)

  

主要问题是我输了EXIF。

嗯,是的。 JPEG文件可能包含EXIF信息。 Bitmap没有。默认情况下,从Bitmap创建的JPEG不会。您需要从原始JPEG中读取EXIF信息,并将相同的信息(或根据需要进行调整)添加到新JPEG中。