当我上传到服务器时相机图像发生旋转

2024-03-13

我要么拍照,要么从图库中选择一张照片,然后按应有的方式在 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);
}

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

//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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当我上传到服务器时相机图像发生旋转 的相关文章

随机推荐

  • Android - 移动网络设置菜单(Jelly Bean)

    以下代码不适用于 Jelly Bean Android 4 1 final ComponentName cn new ComponentName com android phone com android phone Settings fi
  • ios 中纹理的多重采样渲染

    我正在尝试在启用多重采样的 ios 中渲染到纹理 然后在最终输出中使用该纹理 这可能吗 到目前为止 我只得到了黑色纹理或锯齿图像 我正在使用的代码是 glGenTextures 1 texture glBindTexture GL TEXT
  • C# 没有边界检查的 byte[] 比较

    我正在寻找性能高效的方法来比较两个 byte 是否相等 大小超过 1 MB 因此每个数组元素的开销应最小化 我的目标是超越SequenceEqual http msdn microsoft com en us library bb34856
  • python tkinter 列表框事件绑定

    我无法让事件绑定与 python tkinter 一起使用 我只是尝试单击并打印位置 但每次执行此操作时 结果都是 1 这是我的代码 from Tkinter import import Tkinter class make list Tk
  • spring junit 加载应用程序上下文进行测试

    我的 WEB INF 目录下有一些 XML 文件 歌词BaseApp servlet xml 休眠文件 数据源 xml beans xml servlet xml 导入其他 xml 文件
  • 如何旋转图像pygame的蒙版

    您好 我在旋转对象的蒙版时遇到问题 旋转蒙版仍处于与原始图像相同的位置 重点是在赛道上移动掩模以形成碰撞 def init self x y height width self x x width 2 self y y height 2 s
  • Angular - 使用 Restangular 时中止 ajax 请求

    我有一个方法调用角度服务 从而通过该服务发出 ajax 请求 我需要确保如果多次调用此方法 则先前的请求将被中止 如果尚未解决 该方法可以被多次调用 这个方法其实是来自ngTable上的ngTableParams getData funct
  • 如何监听 MongoDB 集合的更改?

    我正在创建一种后台作业队列系统 使用 MongoDB 作为数据存储 在派生工作人员来处理作业之前 如何 侦听 对 MongoDB 集合的插入 我是否需要每隔几秒轮询一次以查看与上次相比是否有任何更改 或者我的脚本是否可以等待插入发生 这是我
  • Web应用程序不会加入Infinispan集群

    我最近一直在玩 Infinispan 之前没有使用 Infinispan 的经验 我遇到了一个有趣的问题 我想知道是否有人能够阐明它 我有一个独立的 Java 应用程序 GridGrabber jar 它捆绑了 Infinispan jar
  • 使用后台附件:在 ipad 上的 safari 中修复

    我希望重新创建类似于科普应用程序的效果 基本上有一个大的背景图像 然后在其上有 HTML CSS 层 当用户滚动内容时 图像的背景位置应保持在原位 而不是滚动 显然 在 常规 浏览器中我会使用background attachment fi
  • NodeJS My SQL 查询与 Chain Promise

    我有3个函数 我想逐步调用这个函数 例如当我调用第一个函数并获取结果时 我必须调用第二个函数并传递从第一次调用返回的参数 在完成第二个调用后 我必须调用第三个函数并传递从第二个函数返回的参数 1 getCategory function b
  • 计算无符号整数中位转换数量的最快方法

    我正在寻找最快的方法来计算位转换的数量unsigned int 如果 int 包含 0b00000000000000000000000000001010 转换次数为 4 如果 int 包含 0b00000000000000000000000
  • 无法更改导航控制器中导航栏的高度(它比正常情况宽得多)

    我有一个从主 TabBarController 扩展并扩展 ViewController 的 NavigationController 和 VC 扩展至 2x TableView 因此 TabBar gt NavigationControl
  • Rails 中的 Object#presence 有什么意义?

    在 Rails 文档中 提供的例子 http api rubyonrails org classes Object html method i presence为了Object presence方法是 region params state
  • 当引导服务器关闭时,具有 transactionIdPrefix 的 DefaultKafkaProducerFactory 会无限等待

    Hy 我正在使用 spring kafka 1 3 0 RELEASE 创建事务生产者 当引导服务器关闭时 DefaultKafkaProducerFactory 会无休止地等待 直到引导服务器启动 我究竟做错了什么 我可以设置超时和 或其
  • 更改 GdkPixbuf (GTK3) 中像素的颜色

    我在用着Gtk StatusIcon 并且想要改变某些像素的颜色 我有一段工作代码 它加载一个带有我想要设置的颜色的 1x1 像素 PNG 文件 然后将其复制到图标 Pixbuf 虽然这种方法有效 但它有一个明显的缺点 即必须为每种颜色创建
  • 尝试在 Windows 2016 Core 容器中创建计划任务时出错

    我正在尝试构建一个包含自定义计划任务的容器 这是我的 dockerfile FROM microsoft windowsservercore RUN schtasks create tn hello sc daily st 00 00 tr
  • PHP登录错误未定义索引

    我正在尝试使用此代码登录 session start require connect php username POST username password POST password if username password query
  • ErrorColumn 值不作为 Lineage ID 存在

    在插入目标表期间 发生的任何错误都会被重定向到错误表 我们可以在其中看到ErrorCode and ErrorColumn 问题是我们得到了一个值ErrorColumn它不存在于包中的任何地方 也就是说 没有一个列具有LineageID等于
  • 当我上传到服务器时相机图像发生旋转

    我要么拍照 要么从图库中选择一张照片 然后按应有的方式在 ImageView 中显示它 就旋转而言 但是 每当我将其上传到服务器时 它总是以横向模式上传 即使它在我的画廊中处于纵向模式 我该如何解决这个问题 private void tak