简体   繁体   English

如何旋转相机拍摄的照片?

[英]How can I rotate the photos taken with the camera?

I inserted on my App a option to take a photo and save it on a directory. 我在应用程序中插入了一个选项,可以拍摄照片并将其保存在目录中。

But when I check the taken photo it appears turned. 但是,当我检查所拍摄的照片时,它似乎已打开。 The horizontal photos appears vertical, and the vertical photos appears horizontal. 水平照片显示为垂直,垂直照片显示为水平。

how can change my code and automatically rotate the photos? 如何更改我的代码并自动旋转照片?

My code: 我的代码:

        if (options[item].equals("Take Photo"))
        {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
            startActivityForResult(intent, 1);
        }

... ...

if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                File f = new File(Environment.getExternalStorageDirectory().toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }
                try {
                    Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            bitmapOptions); 

                    int nh = (int) ( bitmap.getHeight() * (612.0 / bitmap.getWidth()) );
                    Bitmap scaled = Bitmap.createScaledBitmap(bitmap, 612, nh, true);

                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    scaled.compress(Bitmap.CompressFormat.JPEG, 90, stream);

                    byte [] byte_arr = stream.toByteArray();
                    String image_str = Base64.encodeBytes(byte_arr);

                    viewImage.setScaleType(ScaleType.CENTER);
                    viewImage.setImageBitmap(scaled);

                    task = new getinfo();
                    task.execute(image_str);

                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Phoenix" + File.separator + "default";
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        outFile.flush();
                        outFile.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

This piece helped me to do the same task 这件作品帮助我完成了同样的任务

 private Bitmap rotateImage(String pathToImage) {

    // 1. figure out the amount of degrees
    int rotation = getImageRotation();

    // 2. rotate matrix by postconcatination
    Matrix matrix = new Matrix();
    matrix.postRotate(rotation);

    // 3. create Bitmap from rotated matrix
    Bitmap sourceBitmap = BitmapFactory.decodeFile(pathToImage);
    return Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);        
}

Also check this , this and this for more details. 另请检查thisthisthis以获得更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我知道如何打开相机的按钮,但是我不知道如何更改拍摄照片的位置,我该怎么做? - I know how to make a button open the camera, but I don't how to change the location of the photos that are taken, how can I do this? 自动旋转使用智能手机拍摄的照片 - Rotate automatically photos taken with Smartphone 将其推入阵列后,我无法预览相机拍摄的照片吗? - I am not able to preview the photos taken by camera after pushing it to a array? 如何在Android Studio中将从相机拍摄的照片添加到文档模板中 - How to add photos taken from camera to a document template in android studio 如何旋转相机预览以纵向模式拍摄风景照片? - How do I rotate a Camera preview to take landscape photos in portrait mode? 如何显示从相机拍摄的图像 - How can I show image taken from camera 如何将从相机拍摄的图像保存到内部存储器中 - How can I save image taken from camera into internal storage 如何将以相机意图拍摄的图像设置为ImageView? - How can i set an image taken from the Camera intent into a ImageView? 如何在应用程序中编辑通过相机拍摄的照片? - How can I edit a photo taken via the camera in my application? 如何在Android设备上访问相机胶卷/照片? - how can i access the camera roll/Photos on Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM