【问题标题】:Image orientation changes while uploading Image to server将图像上传到服务器时图像方向发生变化
【发布时间】:2017-09-16 11:36:38
【问题描述】:

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

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

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

【问题讨论】:

  • 您的问题是您的图像数据未旋转 - 图像具有指示旋转的 EXIF 方向数据集。有些查看者在显示图像时会考虑 EXIF 方向,有些则不会。为了节省处理时间和内存,大多数移动设备总是以一个特定的(通常是横向)方向写入图像数据,如果您以不同的方式握住相机,只需设置一小块 EX​​IF 元数据来表明这一点,而不是而不是旋转数据本身。
  • @MattGibson 我认为您可以将其发布为答案
  • 我的图片路径在我的手机中显示了正确的图片,那么我怎样才能在没有任何倾斜的情况下将相同的图片上传到服务器。
  • @ntv1000 这可能是原因,但实际上并不能解决问题。我会把它留给其他人,虽然我想已经有一个关于“如何旋转这个图像以匹配它的 EXIF”的现有回答问题......
  • 在手机中是正确的。但是在传输到服务器时它会倾斜。因此,您需要将手机中的图像倾斜并传输到服务器。请参阅我的答案以获取代码。

标签: android


【解决方案1】:

很遗憾,除了加载图像、手动旋转并以正确的方向重新保存之外,没有其他方法可以修改照片文件的方向。

图片旋转示例可以参考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. 上传图片到服务器

【讨论】:

  • @user1761316:答案已更新。您还可以将文件直接发送到服务器,并通过 jquery 或其他方式在您的网页中使用旋转选项,如果您更喜欢这种方式。
  • 我需要上传文件格式,我可以把Bitmap转换成File.multipartContent.addPart("image", new FileBody(new File( al_image_paths.get(i)))))
【解决方案2】:

使用该代码

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

【讨论】:

    猜你喜欢
    • 2022-09-26
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2013-12-17
    • 2014-03-21
    • 2017-05-31
    • 2013-11-02
    • 2014-03-24
    相关资源
    最近更新 更多