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

时间:2015-06-16 21:01:33

标签: android

我在我的应用程序上插入了一张照片并将其保存在目录中的选项。

但是当我检查拍摄的照片时,它似乎已经转动了。水平照片显示为垂直,垂直照片显示为水平。

如何更改我的代码并自动旋转照片?

我的代码:

        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();
                }

1 个答案:

答案 0 :(得分:0)

这篇文章帮助我完成了同样的任务

 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);        
}

另请查看thisthisthis了解详情。

相关问题