我有一个项目。我想拍照使用相机设备和打开画廊拍照,两者都可以在我的项目中的imageview上显示。那么,我希望这个图像在imageview中可以进行,旋转。我有旋转代码,但只为图像完成的代码保存在drawable文件夹中。请帮忙救救我。在XML中任何图像视图(用于显示图像),3按钮:“拍照”,“打开图库”和“旋转”。如何在图库视图中显示的图像可以旋转?
答案 0 :(得分: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();
答案 1 :(得分: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中显示图片。但是,当我点击“旋转”时,picute只是冻结。请修复我的代码......