2015-11-04 98 views
1

我正在創建一個使用相機的應用程序。相機在風景和肖像方面都非常完美。但結果圖像在橫向模式的框架中完美設置。但在肖像中,結果圖像設置在90度方向的框架中以解決這個問題?肖像模式下的相機預覽

public void createImageInImageCenter() { 



    Bitmap backgroundBitmap = DgCamActivity.photo; 



    backgroundBitmap = backgroundBitmap.createScaledBitmap(
      backgroundBitmap, 900, 700, true); 


    Bitmap bitmapToDrawInTheCenter = null; 
    File f = new File(FrameGridView.selected_img); 
    BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    try { 
     bitmapToDrawInTheCenter = BitmapFactory.decodeStream(
       new FileInputStream(f), null, options); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    bitmapToDrawInTheCenter = bitmapToDrawInTheCenter.createScaledBitmap(
      bitmapToDrawInTheCenter, 900, 700, true); 

    resultBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), 
      backgroundBitmap.getHeight(), backgroundBitmap.getConfig()); 
    Canvas canvas = new Canvas(resultBitmap); 
    canvas.drawBitmap(backgroundBitmap, new Matrix(), null); 

    canvas.drawBitmap(bitmapToDrawInTheCenter, 0, 0, new Paint()); 

    ImageView image = (ImageView) findViewById(R.id.final_img); 
    image.setImageBitmap(resultBitmap); 

} 

回答

1

請嘗試下面的代碼,它有助於你處理圖像旋轉。

public class CaptureImage extends Activity { 

private static final int PICK_CAMERA_IMAGE = 2; 

ImageView img; 
Button btn; 
double d = 1.2; 

private Uri mImageCaptureUri; 

public static String userPicPath; 
File file; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_capture_image); 

    img = (ImageView) findViewById(R.id.activity_capture_image_img); 
    btn = (Button) findViewById(R.id.button1); 

    btn.setText(String.valueOf(d)); 

    btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      SimpleDateFormat dateFormatter = new SimpleDateFormat(
        "yyyyMMdd_HHmmss", Locale.US); 

      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
      file = new File(Environment.getExternalStorageDirectory() 
        + "/MyImage", "img_" 
        + dateFormatter.format(new Date()).toString() + ".png"); 
      userPicPath = file.getPath(); 
      mImageCaptureUri = Uri.fromFile(file); 

      intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri); 
      startActivityForResult(intent, PICK_CAMERA_IMAGE); 
     } 
    }); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == PICK_CAMERA_IMAGE && resultCode == RESULT_OK) { 

     Log.d("CaptureImage", mImageCaptureUri.toString()); 

     Bitmap bitmapProfile = getBitmap(userPicPath, this); 

     img.setImageBitmap(rotatedBitmap(file, bitmapProfile)); 

    } 
} 

public static Bitmap rotatedBitmap(File imageFile, Bitmap source) { 

    try { 
     int rotate = 0; 
     ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); 
     int orientation = exif.getAttributeInt(
       ExifInterface.TAG_ORIENTATION, 
       ExifInterface.ORIENTATION_NORMAL); 
     switch (orientation) { 
     case ExifInterface.ORIENTATION_ROTATE_270: 
      rotate = 270; 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_180: 
      rotate = 180; 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      rotate = 90; 
      break; 
     } 

     Log.v("Capture Image", "Exif orientation: " + orientation + ":" 
       + String.valueOf(rotate)); 

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

     return Bitmap.createBitmap(source, 0, 0, source.getWidth(), 
       source.getHeight(), matrix, false); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 

public static Bitmap getBitmap(String path, Context context) { 
    Uri uri = Uri.fromFile(new File(path)); 
    InputStream in = null; 
    ContentResolver mContentResolver = context.getContentResolver(); 
    try { 
     // final int IMAGE_MAX_SIZE = 2048; 
     final int IMAGE_MAX_SIZE = 1024; 
     in = mContentResolver.openInputStream(uri); 

     // Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 

     BitmapFactory.decodeStream(in, null, o); 
     in.close(); 

     int scale = 1; 
     if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) { 
      scale = (int) Math.pow(
        2, 
        (int) Math.round(Math.log(IMAGE_MAX_SIZE 
          /(double) Math.max(o.outHeight, o.outWidth)) 
          /Math.log(0.5))); 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     in = mContentResolver.openInputStream(uri); 
     Bitmap b = BitmapFactory.decodeStream(in, null, o2); 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     b.compress(Bitmap.CompressFormat.JPEG, 25, stream); 
     in.close(); 

     return b; 
    } catch (FileNotFoundException e) { 
     Log.e("CaptureImage", "file " + path + " not found"); 
    } catch (IOException e) { 
     Log.e("CaptureImage", "file " + path + " not found"); 
    } 
    return null; 
} 

}

和佈局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Capture Image" /> 

<ImageView 
    android:id="@+id/activity_capture_image_img" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:scaleType="fitCenter" 
    android:src="@drawable/ic_launcher" /> 

+0

此代碼正確地適合相機拍攝的圖像到框架中.. – Gopal

+0

此代碼已解決你問題,然後請upvote和更正作爲答案 – Shailesh

+0

sry @Shaileshi嘗試此代碼,它不工作。 bitmapToDrawInTheCenter = bitmapToDrawInTheCenter.createScaledBitmap( bitmapToDrawInTheCenter,900,700,true); (), resultBitmap = Bitmap.createBitmap(backgroundBitmap.getWidth(), backgroundBitmap.getHeight(),backgroundBitmap.getConfig()); Canvas canvas = new Canvas(resultBitmap); canvas.drawBitmap(backgroundBitmap,new Matrix(),null); canvas.drawBitmap(bitmapToDrawInTheCenter,0,0,new Paint()); – Gopal

0

我遇到了同樣的問題。在Android API 13之前,相機的預覽圖像與預覽圖像相同,但是在API 13之後,它們已將Y軸從預覽中翻轉過來。這是從相機的角度來看「真實」的觀點。應用此功能,您的位圖:

public Bitmap rotateBitmap(Bitmap source) { 
    orientation = getActivity().getWindowManager().getDefaultDisplay().getRotation(); 
    Matrix matrix = new Matrix(); 

    if(android.os.Build.VERSION.SDK_INT > 13) { 
    float[] mirrorY = {-1, 0, 0, 0, 1, 0, 0, 0, 1}; 
    Matrix matrixMirrorY = new Matrix(); 
    matrixMirrorY.setValues(mirrorY); 
    matrix.postConcat(matrixMirrorY); 
    } 

    switch (orientation) { 
    case Surface.ROTATION_0: 
     if(android.os.Build.VERSION.SDK_INT > 13) { 
     matrix.postRotate(90); 
     } else { 
     matrix.postRotate(270); 
     } 
    break; 

    case Surface.ROTATION_90: 
     matrix.postRotate(0); 
    break; 

    case Surface.ROTATION_180: 
     if(android.os.Build.VERSION.SDK_INT > 13) { 
     matrix.postRotate(270); 
     } else { 
     matrix.postRotate(90); 
     } 
     break; 

     case Surface.ROTATION_270: 
     matrix.postRotate(180); 
     break; 
    } 

    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true); 
} 

這是用於處理圖像或將其保存到SD卡的例子。但是,如果在預覽時的旋轉問題,在您的「surfaceChanged」方法使用旋轉:

Camera.Parameters parameters = mCamera.getParameters(); 
orientation = getActivity().getWindowManager().getDefaultDisplay().getRotation(); 

switch (orientation) { 
    case Surface.ROTATION_0: 
    mCamera.setDisplayOrientation(90); 
    break; 

    case Surface.ROTATION_90: 
    mCamera.setDisplayOrientation(0); 
    break; 

    case Surface.ROTATION_180: 
    mCamera.setDisplayOrientation(270); 
    break; 

    case Surface.ROTATION_270: 
    mCamera.setDisplayOrientation(180); 
    break; 
} 

測試,在Android 4.x版(AOSP,氰,和FirePhone)工作。

+0

我也是用這個方法來相機方向。問題是camera.setImageBitmap(resultBitmap);它在landscape.in肖像圖像適合90度旋轉完美運作。 – Gopal

+0

謝謝.....你的代碼工作正常。但是該框架也旋轉..我只想旋轉相機拍攝的圖像.. – Gopal