2014-02-13 110 views
0

我有應用程序捕獲圖像並將其與覆蓋圖像一起保存。拍攝照片上方的圖像就像中心有透明部分的邊框。問題是,在某些設備上的圖片被打破,看起來像這樣
某些設備上的照相機圖像保存不正確

所以棕色邊界是確定的,但拍攝的圖像是不正常。具體來說,它在HTC sense 3.6(Android 4.0.3) 上重現。首先,我捕獲圖像並創建帶有邊框的位圖。比我將它保存在SD卡上並在下一個活動中顯示它。在這裏我的代碼:

private class CameraPictureCallback implements Camera.PictureCallback { 

    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 

     File pictureFile = PictureStorage.getOutputMediaFile(PictureStorage.MEDIA_TYPE_IMAGE); 
     if (pictureFile == null) { 
      Logger.getInstance().log("Error creating media file, check storage permissions: "); 
      return; 
     } 

     if (data != null) { 

      Bitmap border = BitmapFactory.decodeResource(getResources(), R.drawable.photo_frame); 

      BitmapFactory.Options opts = new BitmapFactory.Options(); 
      opts.inSampleSize = 4; 
      Bitmap origin_bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts); 

      Matrix matrix = new Matrix(); 
      matrix.postRotate(mRotation); 

      //Border is 640x640px 
      Bitmap bitmap = Bitmap.createScaledBitmap(origin_bitmap, border.getWidth(), border.getHeight(), false); 
      bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 

      Bitmap resultBitmap = Bitmap.createBitmap(border.getWidth(), border.getHeight(), Bitmap.Config.ARGB_8888); 

      Canvas s = new Canvas(resultBitmap); 
      s.drawBitmap(bitmap, 0f, 0f, null); 
      s.drawBitmap(border, 0f, 0f, null); 

      try { 
       FileOutputStream fos = new FileOutputStream(pictureFile); 
       if (resultBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos)) { 
        fos.close(); 
       } 
       resultBitmap.recycle(); 
       bitmap.recycle(); 
       border.recycle(); 
       origin_bitmap.recycle(); 

       Intent i = new Intent(getContext(), PictureConfirmActivity.class); 
       i.putExtra("filename", pictureFile.getPath()); 
       getContext().startActivity(i); 
      } catch (FileNotFoundException e) { 
       Logger.getInstance().log(e); 
      } catch (IOException e) { 
       Logger.getInstance().log(e); 
      } 
     } 
    } 
} 

它不會複製任何我的設備上,但問題是很實際的,我需要解決它。我可以想象它可以在這裏,resultBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos),但它只是一個想法

回答

0

問題是與設置正確的預覽大小。我選擇了第一個可用的屏幕尺寸,這會導致不可預知的結果。所以正確的解決方案是找到合適的預覽尺寸

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
    //some code before... 
    Camera.Parameters parameters = mCamera.getParameters(); 
    Camera.Size previewSize = getBestPreviewSize(w, h, parameters); 
    parameters.setPreviewSize(previewSize.width, previewSize.height); 
    mCamera.setParameters(parameters); 
} 

private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) { 
    Camera.Size result = null; 
    for (Camera.Size size : parameters.getSupportedPreviewSizes()) { 
     if (size.width <= width && size.height <= height) { 
      if (result == null) { 
       result = size; 
      } 
      else { 
       int resultArea = result.width * result.height; 
       int newArea = size.width * size.height; 

       if (newArea > resultArea) { 
        result = size; 
       } 
      } 
     } 
    } 

    if (result == null) { 
     result = parameters.getSupportedPreviewSizes().get(0); 
    } 

    return result; 
} 
相關問題