Android相机将图片作为肖像返回,将其视为风景?

时间:2015-10-10 14:37:49

标签: java android android-camera

 Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);

        startActivityForResult(i, CAMERA_RESULT);

这就是我在我的应用程序中调用相机的方式,因为我需要高质量的输出,我在中间使用contentProvider

Bitmap mBitmap = BitmapFactory.decodeFile(out.getAbsolutePath());

        ImageView im1 = (ImageView)findViewById(R.id.camTemp);
        im1.setImageBitmap(mBitmap);

onActivityResult(),这就是我显示拍摄图像的方式。

介于两者之间,这是contentProvider类的代码。

public class MyFileContentProvider extends ContentProvider {
public static final Uri CONTENT_URI = Uri.parse
        ("content://obx.com.futurister/");
private static final HashMap<String, String> MIME_TYPES =
        new HashMap<String, String>();

static {
    MIME_TYPES.put(".jpg", "image/jpeg");
    MIME_TYPES.put(".jpeg", "image/jpeg");
}

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    // Implement this to handle requests to delete one or more rows.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public String getType(Uri uri) {
    String path = uri.toString();

    for (String extension : MIME_TYPES.keySet()) {
        if (path.endsWith(extension)) {
            return (MIME_TYPES.get(extension));
        }
    }
    return (null);
}

@Override
public Uri insert(Uri uri, ContentValues values) {
    // TODO: Implement this to handle requests to insert a new row.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public boolean onCreate() {
    try {
        File mFile = new File(getContext().getFilesDir(), "newImage.jpg");
        if(!mFile.exists()) {
            mFile.createNewFile();
        }
        getContext().getContentResolver().notifyChange(CONTENT_URI, null);
        return (true);
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }

}

@Override
public ParcelFileDescriptor openFile(Uri uri, String mode)
        throws FileNotFoundException {

    File f = new File(getContext().getFilesDir(), "newImage.jpg");
    if (f.exists()) {
        return (ParcelFileDescriptor.open(f,
                ParcelFileDescriptor.MODE_READ_WRITE));
    }
    throw new FileNotFoundException(uri.getPath());
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {
    // TODO: Implement this to handle query requests from clients.
    throw new UnsupportedOperationException("Not yet implemented");
}

@Override
public int update(Uri uri, ContentValues values, String selection,
                  String[] selectionArgs) {
    // TODO: Implement this to handle requests to update one or more rows.
    throw new UnsupportedOperationException("Not yet implemented");
}

}

现在,问题是,我最终在ImageView中获得的图片是portrait,默认情况下如何将其作为landscape获取?

1 个答案:

答案 0 :(得分:1)

您可以轻松旋转肖像图像并获得横向图像!

Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap , 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);