为什么来自画廊的肖像图像被保存为Parse作为景观?

时间:2015-01-18 16:24:10

标签: java android parse-platform image-uploading image-rotation

我从 @Haresh Chhelana answer找到了以下代码并对其进行了一些修改,以便旋转从图库中选择的照片或使用Android手机拍摄的照片,然后将其保存到Parse云。


当我从相机拍摄照片时,使用横向或纵向方向,当我从Parse检查时,它总是看起来正确。

当我从图库中选择风景照片时,照片正在再次正确保存。

但是,当我尝试保存肖像照片时,照片正在保存风景

请注意,在我的清单中我有这些行,所以我的应用程序总是以纵向方式运行(我不知道这是否与问题有关):

android:configChanges="orientation" android:screenOrientation="portrait"


当用户想要从图库上传图片或拍摄新图片时,用户按下按钮的听众:

picture.setOnClickListener(new View.OnClickListener() {
                @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
                    flag_photo = true;
                    }
            });

            camera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                    startActivityForResult(intent, CAPTURE_IMAGE);
                }
            });

我将照片保存在我的一个功能中的方式:

ParseFile file = null;

            file = new ParseFile("profile_picture.jpg", image);

            // Upload the image into Parse Cloud
            file.saveInBackground();

            user.put("photo", file);

OnActivityResult:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CAPTURE_IMAGE) {
                rotateImage(getImagePath());
            } else if (requestCode == PICK_IMAGE) {
//                imgFromCameraOrGallery.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData())));

                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                BitmapFactory.decodeFile(getAbsolutePath(data.getData())).compress(Bitmap.CompressFormat.JPEG, 100, stream);
                image = stream.toByteArray();

            }
            flag_photo = true;
        }
        else
            flag_photo = false;
    }

RotateImage功能:

  private void rotateImage(final String path) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Bitmap b = decodeFileFromPath(path);
                try {
                    ExifInterface ei = new ExifInterface(path);
                    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                    Matrix matrix = new Matrix();
                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            matrix.postRotate(90);
                            b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            matrix.postRotate(180);
                            b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            matrix.postRotate(270);
                            b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
                            break;
                        default:
                            b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
                            break;
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                }


                try {
                    String state = Environment.getExternalStorageState();
                    if (Environment.MEDIA_MOUNTED.equals(state)) {
                        file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");
                    }
                    else {
                        file = new File(getFilesDir() , "image" + new Date().getTime() + ".jpg");
                    }
                    out1 = new FileOutputStream(file);
                    b.compress(Bitmap.CompressFormat.JPEG, 100, out1);

                    // Convert it to byte
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();

                    BitmapFactory.decodeFile(file.getAbsolutePath()).compress(Bitmap.CompressFormat.JPEG, 100, stream);

                    // Compress image to lower quality scale 1 - 100
                    image = stream.toByteArray();

                    //imgFromCameraOrGallery.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out1.close();
                    } catch (Throwable ignore) {

                    }
                }

            }
        });

    }

这就是DecodeFileFromPath:

 private Bitmap decodeFileFromPath(String path){
        Uri uri = getImageUri(path);
        InputStream in = null;
        try {
            in = getContentResolver().openInputStream(uri);

            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

            BitmapFactory.decodeStream(in, null, o);
            in.close();


            int scale = 1;
            int inSampleSize = 1024;
            if (o.outHeight > inSampleSize || o.outWidth > inSampleSize) {
                scale = (int) Math.pow(2, (int) Math.round(Math.log(inSampleSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            in = getContentResolver().openInputStream(uri);
            Bitmap b = BitmapFactory.decodeStream(in, null, o2);
            in.close();

            return b;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

如果你想查看他们的代码,我也有这些功能。我没有发布它们,因为我的问题变得太长了,我不相信我可以改变它们来解决我的问题: enter image description here

0 个答案:

没有答案