将图像上载到服务器时图像方向发生变化

时间:2014-07-08 10:34:30

标签: android

我使用以下代码将图片上传到服务器,它已成功上传,但图片方向更改为-90。

我不明白如何解决这个问题。我在Sdcard中的图像方向正确,但我不知道为什么这个图像会改变它的方向。

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(image_upload);

Log.e("strImagePath.... before uploading", al_image_paths.get(i));


multipartContent.addPart("image", new FileBody(new File(al_image_paths.get(i))));

multipartContent.addPart("sellleadid", new StringBody("2234"));
multipartContent.addPart("action", new StringBody("Send Used Car Images"));
multipartContent.addPart("app_id", new StringBody("1"));

totalSize = multipartContent.getContentLength();

httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost, localContext);
String serverResponse = EntityUtils.toString(response.getEntity());
Log.e("serverResponse image", "<> " + serverResponse);

2 个答案:

答案 0 :(得分:3)

遗憾的是,除了加载图像,手动旋转图像文件以及正确的方向重新保存图像文件之外,无法修改照片文件的方向。

您可以参考this代码查看图像旋转样本:

int rotate = 0;
try {
    getContentResolver().notifyChange(imageUri, null);
    File imageFile = new File(al_image_paths.get(i)); 
    ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
    }
    Log.v(Common.TAG, "Exif orientation: " + orientation);

    Bitmap rotattedBitmap= BitmapFactory.decodeFile(al_image_paths.get(i));           
    Matrix matrix = new Matrix();
    matrix.postRotate(rotate);
    return Bitmap.createBitmap(rotattedBitmap, 0, 0, rotattedBitmap.getWidth(), rotattedBitmap.getHeight(), matrix, true);
} catch (Exception e) {
    e.printStackTrace();
}

修改 在您的情况下,您必须执行以下操作:

  1. 识别图像EXIF方向
  2. 创建位图
  3. 旋转位图
  4. 将位图另存为图像
  5. 将图片上传到服务器

答案 1 :(得分:0)

使用该代码

ExifInterface exif;
int angle = 0;
try {
    exif = new ExifInterface(myUri.getPath());
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
        angle = 90;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
        angle = 180;
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Matrix  matrix1 = new Matrix();

//set image rotation value to 45 degrees in matrix.
matrix1.postRotate(angle);

//Create bitmap with new values.
Bitmap photo = Bitmap.createBitmap( bitmap, 0, 0, bitmap.getWidth(),  bitmap.getHeight(), matrix1, true);