【问题标题】:Camera image gets rotated when I upload to server当我上传到服务器时,相机图像被旋转
【发布时间】:2017-01-23 17:55:57
【问题描述】:

我要么拍照,要么从图库中选择一张照片,并将其显示在 ImageView 中(就旋转而言)。但是,每当我将它上传到服务器时,它总是以横向模式上传,即使它在我的画廊中是纵向模式。我该如何解决这个问题?

private void takePhoto() {
    Intent takePhoto = new Intent();
    takePhoto.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    File photoFile = null;
    try {
        photoFile = imagePath();
    } catch (IOException e) {
        Log.d(TAG, "Take Photo: " + e.getMessage());
    }
    takePhoto.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
    startActivityForResult(takePhoto, REQUEST_IMAGE);
}

private File imagePath() throws IOException {
    String timeStamp = new java.text.SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    String imageFileName = "IMAGE_" + timeStamp + "_";
    File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(imageFileName, ".jpg", storageDirectory);
    mImageLocation = image.getAbsolutePath();
    return image;
}

private void uploadMultipart() {
    String name = etName.getText().toString();
    String path = mImageLocation;

    try {
        String uploadId = UUID.randomUUID().toString();
        new MultipartUploadRequest(this, uploadId, API.IMAGE_UPLOAD_URL)
                .addFileToUpload(path, "image")
                .addParameter("name", name)
                .setNotificationConfig(new UploadNotificationConfig())
                .setMaxRetries(2)
                .startUpload();
    } catch (Exception e) {
        Log.d(TAG, "Upload: " + e.getMessage());
    }
}

private Bitmap setReducedImageSize() {
    int targetImageViewWidth = capturedPhoto.getWidth();
    int targetImageViewHeight = capturedPhoto.getHeight();

    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(mImageLocation, bmOptions);

    int cameraImageWidth = bmOptions.outWidth;
    int cameraImageHeight = bmOptions.outHeight;

    int scaleFactor = Math.min(cameraImageWidth / targetImageViewWidth, cameraImageHeight / targetImageViewHeight);
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inJustDecodeBounds = false;

    /*Bitmap reducedPhoto = BitmapFactory.decodeFile(mImageLocation, bmOptions);
    capturedPhoto.setImageBitmap(reducedPhoto);*/
    return BitmapFactory.decodeFile(mImageLocation, bmOptions);
}

private void rotateImage(Bitmap bitmap) {
    ExifInterface exifInterface = null;
    try {
        exifInterface = new ExifInterface(mImageLocation);
    } catch (IOException e) {
        Log.d(TAG, "Rotate Image: " + e.getMessage());
    }
    int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
    Matrix matrix = new Matrix();
    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.setRotate(90);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.setRotate(180);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.setRotate(270);
            break;
        default:
    }
    Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    capturedPhoto.setImageBitmap(rotatedBitmap);
}

【问题讨论】:

标签: android multipartform-data image-rotation


【解决方案1】:

几周前我花了一些时间面对同样的问题。我做了一些挖掘,这就是我所做的让我的照片始终以正确的方向上传:)。它每次都适用于每台设备。希望对您有所帮助。

//this is the byte stream that I upload.
public static byte[] getStreamByteFromImage(final File imageFile) {

    Bitmap photoBitmap = BitmapFactory.decodeFile(imageFile.getPath());
    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    int imageRotation = getImageRotation(imageFile);

    if (imageRotation != 0)
        photoBitmap = getBitmapRotatedByDegree(photoBitmap, imageRotation);

    photoBitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);

    return stream.toByteArray();
}



private static int getImageRotation(final File imageFile) {

    ExifInterface exif = null;
    int exifRotation = 0;

    try {
        exif = new ExifInterface(imageFile.getPath());
        exifRotation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (exif == null)
        return 0;
    else
        return exifToDegrees(exifRotation);
}

private static int exifToDegrees(int rotation) {
    if (rotation == ExifInterface.ORIENTATION_ROTATE_90)
        return 90;
    else if (rotation == ExifInterface.ORIENTATION_ROTATE_180)
        return 180;
    else if (rotation == ExifInterface.ORIENTATION_ROTATE_270)
        return 270;

    return 0;
}

private static Bitmap getBitmapRotatedByDegree(Bitmap bitmap, int rotationDegree) {
    Matrix matrix = new Matrix();
    matrix.preRotate(rotationDegree);

    return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

【讨论】:

  • 对不起,回答迟了,但你用文件作为参数来做,而我用路径作为参数来做。如何使用路径实现您的代码?
  • 只需从路径中获取 File 对象并执行相同操作吗?文件照片 = 新文件(PATH_TO_FILE);您还可以注意到,除了 file.getPath() 之外,我没有使用任何其他文件引用,因此您可以将每次出现的 imageFile.getPath() 替换为 your_path。应该很简单:)
  • 您能否告诉我您使用什么类型的连接将图像上传到服务器?
  • HTTP Post 正文中带有照片字节流。
  • @Rybzor 可以用相机和画廊吗?我也面临与三星设备相同的问题。在 moto 系列中它工作正常。
猜你喜欢
  • 2015-06-29
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-30
  • 2018-01-05
相关资源
最近更新 更多