上传到服务器时,相机图像会旋转

时间:2016-09-15 16:03:46

标签: android multipartform-data image-rotation

我要么拍摄照片,要么从照片库中选择一张照片并在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);
}

1 个答案:

答案 0 :(得分:0)

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

=IFERROR('[KPI.xls]Sheet1'!F192&"", "N/A")
'alternate that makes inserting formula via VBA a little easier
=IFERROR('[KPI.xls]Sheet1'!F192&TEXT(,), "N/A")