2014-09-25 79 views
1

我正在創建我的相機表面,但橫向和縱向兩個預覽都拉伸像圖像給出如下。請幫我設置正確的參數。創建全屏幕相機表面視圖沒有拉伸

Snapshot of camera surface

getWindow().setFormat(PixelFormat.UNKNOWN); 
      surfaceView = (SurfaceView)findViewById(R.id.surfaceview); 
      surfaceHolder = surfaceView.getHolder(); 
      surfaceHolder.addCallback(this); 
      surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 


camera = Camera.open(); 

camera.setPreviewDisplay(surfaceHolder); 

回答

1

你可以應用適當的MatrixSurfaceView,我用下面的方法來固定我的圖片:

// Adjustment for orientation of images 
public static Matrix adjustOrientation(SurfaceView preview) { 
    Matrix matrix = new Matrix(preview.getMatrix()); 
    try { 
     ExifInterface exifReader = new ExifInterface(path); 

     int orientation = exifReader.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, -1); 

     if (orientation == ExifInterface.ORIENTATION_NORMAL) { 
      // do nothing 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
      matrix.postRotate(90); 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { 
      matrix.postRotate(180); 
     } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
      matrix.postRotate(270); 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return matrix; 
} 

這得到了Bitmap正確的方向。然後,您可以根據您的寬度和高度,通過以下方法重新縮放Bitmap

public static Bitmap getScaledBitmap(int bound_width, int bound_height, String path) { 

    int new_width; 
    int new_height; 
    Bitmap original_img; 

    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 

    original_img = BitmapFactory.decodeFile(path, options); 
    int original_width = options.outWidth; 
    int original_height = options.outHeight; 

    new_width = original_width; 
    new_height = original_height; 

    // check if we need to scale the width 
    if (original_width > bound_width) { 
     // scale width to fit 
     new_width = bound_width; 
     // scale height to maintain aspect ratio 
     new_height = (new_width * original_height)/original_width; 
    } 

    // check if we need to scale even with the new height 
    if (new_height > bound_height) { 
     // scale height to fit instead 
     new_height = bound_height; 
     // adjust width, keep in mind the aspect ratio 
     new_width = (new_height * original_width)/original_height; 
    } 

    Bitmap originalBitmap = BitmapFactory.decodeFile(path); 
    Bitmap resizedBitmap = Bitmap.createScaledBitmap(originalBitmap, new_width, new_height, true); 
    return resizedBitmap; 
} 

希望這有助於。