为什么当我采用照片肖像模式时,它会被错误地保存?

时间:2014-01-14 02:34:47

标签: android bitmap camera rotation orientation

此部分无效,如何检测当前相机位置是横向还是纵向? 我知道如何旋转图片问题是检测高度或重量是否更高然后更改图像属性...

File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "MyCameraApp");                    


                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                    Uri.parse("file://"+ mediaStorageDir)));

//这里发生了什么?

private PictureCallback mPicture = new PictureCallback()
{
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @Override
    public void onPictureTaken(byte[] data, Camera camera){

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);    

        try
        {
            FileOutputStream fos = new FileOutputStream(pictureFile);

            //if(mCamera.getSceneMode() == SCENE_MODE_PORTRAIT)
            //{
                //Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
                //bmp = rotateImage(90, bmp);
                //ByteArrayOutputStream stream = new ByteArrayOutputStream();
                //bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                //data = stream.toByteArray();
            //}

            fos.write(data);
            fos.close();    

            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "MyCameraApp");




                    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                    Uri.parse("file://"+ mediaStorageDir)));
        }

        catch(FileNotFoundException e)
        {
            Log.d(TAG, "File not found: "+e.getMessage());
        }
        catch(IOException e)
        {
            Log.d(TAG, "Error accessing file: "+e.getMessage());
        }
    }
};

public Bitmap rotateImage(int angle, Bitmap bitmapSrc) 
{
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(bitmapSrc, 0, 0, 
    bitmapSrc.getWidth(), bitmapSrc.getHeight(), matrix, true);
}

public static boolean isSdPresent() {   
    return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
}

private static File getOutputMediaFile(int type){
    File mediaFile = null; 
    if(isSdPresent() == false)
    {
        Log.d(TAG, "There is no Sd card. Cannot use the camera");
    }

   else
    {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),".");

        if(!mediaStorageDir.exists())
        {
            if(!mediaStorageDir.mkdirs())
            {
                Log.d("WorldCupApp", "failed to create directory");
                return null;
            }
        }

         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());       
         if (type == MEDIA_TYPE_IMAGE)
         {        
             mediaFile = new File(mediaStorageDir.getPath() + "IMG_"+ timeStamp + ".jpg");    
         } 
         else 
         {       
             return null;  
         }          
    }
    return mediaFile;
}       

}

0 个答案:

没有答案