Android呼び出しシステムカメラ写真処理写真回転問題

7302 ワード

システムカメラを呼び出して撮影していたところ、突如機種によっては撮影された写真が元の角度ではなく90度回転したのか180度回転したのかが判明し、この問題に合うように次のようになった.
システムカメラを呼び出す:
/**
 *             
 */
private void showCamera(int cameraType) {

    if (!Environment.MEDIA_MOUNTED.equals(Environment
            .getExternalStorageState())) {
        Toast.makeText(getActivity(), "    SDCard,        ",
                Toast.LENGTH_LONG).show();
        return;
    }

    Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    long time = currentTimeMillis();
    String imgName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(time);
    String path = Environment.getExternalStorageDirectory()
            + File.separator + "DCIM" + File.separator + "Camera"
            + File.separator;
    File file = new File(path);
    if (!file.exists()) {
        file.mkdir();
    }

    cameraFilePath = path + imgName + ".jpg"; //        +    +    
    File out = new File(cameraFilePath);
    if (!out.exists()) {
        try {
            out.createNewFile();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    Uri uri = Uri.fromFile(out);
    imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    MediaScannerConnection.scanFile(getActivity(),
            new String[]{cameraFilePath}, null, null);
    startActivityForResult(imageCaptureIntent, cameraType);
}

カメラのコールバックを書き換える:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == -1) {
        switch (requestCode) {

            case 102:
                //      
                //        
                //    
                //       
                String rotatePhoto = PhotoBitmapUtils.amendRotatePhoto(cameraFilePath, getActivity(), false);
                break;
            default:
                break;
        }
    } else {
        switch (requestCode) {
            case 102:
                //     
                File file = new File(cameraFilePath);
                if (file.exists()) {
                    file.delete();
                }
                break;

            default:
                break;
        }
    }

}

検出方法:
    /**
     *         
     *      
     *
     * @param originpath        
     * @param context          
     * @param isReplaceFile           true    false           
     * @return             
     */
    public static String amendRotatePhoto(String originpath, Context context, boolean isReplaceFile) {
        return amendRotatePhoto(originpath, context, false, isReplaceFile);
    }

    /**
     *         
     *      
     *            
     *
     * @param originpath
     * @param context
     * @return
     */
    public static String amendRotatePhoto(String originpath, Context context) {
        return amendRotatePhoto(originpath, context, false, true);
    }

    /**
     *         
     *
     * @param originpath        
     * @param context          
     * @param isCompress        
     * @param isReplaceFile           true    false           
     * @return             
     */
    public static String amendRotatePhoto(String originpath, Context context, boolean isCompress, boolean isReplaceFile) {

        if (TextUtils.isEmpty(originpath)) return originpath;

        //         
        int angle = readPictureDegree(originpath);

        //    
        Bitmap bmp = null;
        if (isCompress) {
            //         Bitmap  
            bmp = getCompressPhoto(originpath);
        }

        if (bmp != null) {
            //    
            Bitmap bitmap = null;
            if (angle != 0) {
                //           
                bitmap = rotaingImageView(angle, bmp);
            }
            if (bitmap != null) {

            }
            //                    
            return savePhotoToSD(bitmap, originpath, context, isReplaceFile);
        } else {
            Bitmap localBitmap = getLocalBitmap(originpath);
            if (localBitmap == null) return originpath;
            //    
            Bitmap bitmap = null;
            if (angle != 0) {
                //           
                bitmap = rotaingImageView(angle, localBitmap);
            }
            if (bitmap != null) {
                return savePhotoToSD(bitmap, originpath, context, isReplaceFile);
            } else {
                return originpath;
            }
        }
    }

    /**
     *         
     *
     * @param path     
     * @return   
     */
    public static int readPictureDegree(String path) {
        int degree = 0;
        try {
            ExifInterface exifInterface = new ExifInterface(path);
            int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    degree = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    degree = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    degree = 270;
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return degree;
    }

/*     
 *
 * @param angle       
 * @param bitmap     
 * @return       
 */
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
    Bitmap returnBm = null;
    //       ,      
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    try {
        //                ,       
        returnBm = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    } catch (OutOfMemoryError e) {
    }
    if (returnBm == null) {
        returnBm = bitmap;
    }
    if (bitmap != returnBm) {
        bitmap.recycle();
    }
    return returnBm;
}

 /**
 *   Bitmap   SD  
 *     SD       
 *
 * @param mbitmap            Bitmap  
 * @param originpath          
 * @param isReplaceFile        
 * @return             ,     null
 */
public static String savePhotoToSD(Bitmap mbitmap, String originpath, Context context, boolean isReplaceFile) {
    FileOutputStream outStream = null;
    String fileName = "";
    if (mbitmap == null) return originpath;
    if (isReplaceFile) {
        fileName = getPhotoFileName(context);
    } else {
        if (TextUtils.isEmpty(originpath)) return originpath;
        fileName = originpath;
    }
    try {
        outStream = new FileOutputStream(fileName);
        //        ,100     
        mbitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        return fileName;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (outStream != null) {
                //       !
                outStream.close();
            }
            if (mbitmap != null) {
                mbitmap.recycle();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

参照先:
https://blog.csdn.net/zhuwentao2150/article/details/51999908