当前位置:操作系统 > 安卓/Android >>

Android 相机在Portrait模式下照相保存照片

在使用android的camera的时候会遇到两个问题,一个是camera在preview的时候orientation的问题,第二个就是在takePicture之后回遇到保存下来的图片旋转90度的问题

先解决第一个preview的orientation的问题,第一:在android2.2与以后的sdk版本中camera的orientation都是用的landscape,如果你的activity的screenOrientation设置成landscape的话,就不会有这个问题;第二:如果你的activity必须用portrait,那么可以在调用camera.open()方法之后调用下面这个方法来解决这个问题,
[java]  
camera.setDisplayOrientation(90); 

如果你用的sdk是2.2之前的话,可以这样解决:
[java] 
Method rotateMethod = android.hardware.Camera.class.getMethod("setDisplayOrientation", int.class); 
rotateMethod.invoke(camera, 90); 


下面就是如何解决保存Portrait模式下图片旋转90度的问题
下面的程序在onPictureTaken方法里面执行
[java] 
BitmapFactory.Options options = new BitmapFactory.Options(); 
            options.inSampleSize = 6; 
            options.inDither = false; 
            options.inPurgeable = true; 
            options.inInputShareable = true; 
[java] 
<span style="white-space:pre">      </span>options.inTempStorage = new byte[32 * 1024]; 
[java] 
       options.inPreferredConfig = Bitmap.Config.RGB_565; 
       Bitmap bMap; 
       bMap = BitmapFactory.decodeByteArray(imgData[0], 0, imgData[0].length, options); 
       if(bMap.getHeight() < bMap.getWidth()){ 
           orientation = 90; 
       } else { 
           orientation = 0;   www.zzzyk.com
       } 
 
       Bitmap bMapRotate; 
       if (orientation != 0) { 
           Matrix matrix = new Matrix(); 
           matrix.postRotate(orientation); 
           bMapRotate = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), 
                   bMap.getHeight(), matrix, true); 
       } else 
           bMapRotate = Bitmap.createScaledBitmap(bMap, bMap.getWidth(), 
                   bMap.getHeight(), true); 
 
 
       FileOutputStream out; 
       try { 
        File imgFile = new File("/xxxx/xxx/snap.jpeg"); 
           out = new FileOutputStream(imgFile); 
           bMapRotate.compress(Bitmap.CompressFormat.JPEG, 90, out); 
           if (bMapRotate != null) { 
               bMapRotate.recycle(); 
               bMapRotate = null; 
           } 
<span style="white-space:pre">  </span>camera.startPreview(); 
       } catch (FileNotFoundException e) { 
           e.printStackTrace(); 
       } 


作者:hundsong
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,