【问题标题】:Image orientation changed on web after uploading from phone?从手机上传后,网络上的图像方向发生了变化?
【发布时间】:2015-10-13 11:23:48
【问题描述】:

我可以从我的手机成功上传一张图片,但是当我在笔记本电脑上看到个人资料时,方向发生了变化。

$upload_path = 'uploads/';
    $filename = $_FILES['userfile']['name'];
    $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
    if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize)
        header("Location: profileOne.php?error=PICTURE");
        //die('The file you attempted to upload is too large.');
    if(!is_writable($upload_path))
      die('You cannot upload to the specified directory, please CHMOD it to 777.');
    if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path .$user_id.".jpg")) {   

        $check=@copy($upload_path .$user_id.'.jpg', '../backend/data/profile_image/'.$user_id.'.jpg');
        if($check){
            $postdata1 = http_build_query(
                array(
                    'user_id' => $user_id,
                    'img'=>'data/profile_image/'.$user_id.'.jpg',
                    'type'=>'web'

                )
            );
            $opts1 = array('http' =>
                array(
                    'method'  => 'POST',
                    'header'  => 'Content-type: application/x-www-form-urlencoded',
                    'content' => $postdata1
                )
            );
            $context1  = stream_context_create($opts1);
            $endPoint1='http://www.ridorama.com/backend/registration/update_profile/';
            $result1 = file_get_contents($endPoint1, false, $context1);

【问题讨论】:

  • 设备摄像头的图像?
  • 是的,我从移动设备拍摄的图像。

标签: android image-uploading image-upload


【解决方案1】:

我认为问题出在设备而不是服务器上,因为拍摄照片的是设备,而应用旋转的是设备......然后问题是您没有在图像统计信息中正确保存旋转以及当您尝试从位图类获取字节或传递您的图像,例如,由于图像在 stats => rotation = 0º 中没有正确应用旋转。

要正确解决此问题,您需要在相机中正确应用旋转,当您拍摄照片时,此选项会将相机旋转保存在照片统计信息中,并且始终将图像转换为字节或位图类或发送照片对于您的服务器,这一点尊重照片中的轮换统计数据。按照下面的代码,将此代码应用于相机预览类:

public static void setCameraDisplayOrientation(Activity activity, int cameraId, android.hardware.Camera camera) {
    android.hardware.Camera.CameraInfo info =
            new android.hardware.Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraId, info);
    int rotation = activity.getWindowManager().getDefaultDisplay()
            .getRotation();

    int degrees = 0;
    switch (rotation) {
        case Surface.ROTATION_0: degrees = 0; break;
        case Surface.ROTATION_90: degrees = 90; break;
        case Surface.ROTATION_180: degrees = 180; break;
        case Surface.ROTATION_270: degrees = 270; break;
    }

    int result;
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
        result = (info.orientation + degrees) % 360;
        result = (360 - result) % 360;  // compensate the mirror
    }
    else {  // back-facing
        result = (info.orientation - degrees + 360) % 360;
    }

    Camera.Parameters parameters = camera.getParameters();
    parameters.setRotation(result);
    camera.setParameters(parameters);

    camera.setDisplayOrientation(result);
}

在 setCameraDisplayOrientation() 方法和照片保存方向正确应用此代码,当您将图像发送到服务器时,服务器也有正确的方向。

告诉我我是否帮助过你并且编程很好!

【讨论】:

  • 如果您在尝试在 Android 设备中查看照片时正确应用代码,请打开照片详细信息,您可以看到具有图像的不同旋转。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
相关资源
最近更新 更多