0

我有一个项目。我想拍照使用相机设备并打开画廊拍照,两者都可以在我的项目中的 imageview 上显示。然后,我想要在 imageview 中可以处理、旋转的图像。我有要旋转的代码,但是仅为图像完成的代码保存在可绘制文件夹中。请帮助并拯救我。在 XML 中任何 imageview(用于显示图像),3 个按钮:“拍照”、“打开画廊”和“旋转”。如何旋转画廊和相机的图像视图中显示的图像?

4

2 回答 2

1

您用于旋转图像的代码是正确的。但我不明白你为什么要将图像缩放到一半。

matrix.postScale(0.5f, 0.5f)移除和改变你会更好

int newWidth = bMap.getWidth()/2;
int newHeight = bMap.getHeight()/2;

int newWidth = bMap.getHeight();
int newHeight = bMap.getWidth();
于 2013-09-11T16:07:05.993 回答
0

这是我的代码:

public class MainActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
private static final int TAKE_PICTURE = 0;
private Uri mUri;
private Bitmap mPhoto;
private static int RESULT_LOAD_IMAGE = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    View tombolCamera = findViewById(R.id.btnCamera);
    tombolCamera.setOnClickListener(this);
    View tombolGallery = findViewById(R.id.btnGallery);
    tombolGallery.setOnClickListener(this);
    View tombolRotate = findViewById(R.id.btnRotate);
    tombolRotate.setOnClickListener(this);
}

public void onClick(View v) {
    switch (v.getId()) {

case R.id.btnCamera:
    Intent c = new Intent("android.media.action.IMAGE_CAPTURE");
    File f = new File(Environment.getExternalStorageDirectory(),  "photo.jpg");
    c.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
    mUri = Uri.fromFile(f);
    startActivityForResult(c, TAKE_PICTURE);
break;

case R.id.btnGallery:
    Intent g = new Intent(
    Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(g, RESULT_LOAD_IMAGE);
break;

case R.id.btnRotate:
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    Bitmap bMap = Bitmap.createBitmap(imageView.getDrawingCache());
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    matrix.postScale(0.5f, 0.5f);

    int newWidth = bMap.getWidth()/2;
    int newHeight = bMap.getHeight()/2;

    Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0, newWidth, newHeight, matrix, true);
    imageView.setImageBitmap(bMapRotate);

break;
    }
}

public void onActivityResult1(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
        if (resultCode == Activity.RESULT_OK) {
            getContentResolver().notifyChange(mUri, null);
            ContentResolver cr = getContentResolver();
            try {
                mPhoto = android.provider.MediaStore.Images.Media.getBitmap(cr, mUri);
                ((ImageView)findViewById(R.id.imgView)).setImageBitmap(mPhoto);
            } catch (Exception e) {
                Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

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

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
    Uri selectedImage = data.getData();
    String[] filePathColumn = { MediaStore.Images.Media.DATA };

    Cursor cursor = getContentResolver().query(selectedImage,
            filePathColumn, null, null, null);
    cursor.moveToFirst();

    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();

    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}

} }

这只是在 imageview 中显示图片的工作按钮“Gallery”。但是,当我单击“旋转”时,图片就会冻结。请修复我的代码....

于 2013-09-11T08:37:18.297 回答