簡體   English   中英

如何旋轉相機拍攝的照片?

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

我在應用程序中插入了一個選項,可以拍攝照片並將其保存在目錄中。

但是,當我檢查所拍攝的照片時,它似乎已打開。 水平照片顯示為垂直,垂直照片顯示為水平。

如何更改我的代碼並自動旋轉照片?

我的代碼:

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

這件作品幫助我完成了同樣的任務

 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以獲得更多詳細信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM