Reputation: 11
I have a project. i want take picture use camera device and open gallery to take picture and both can show on imageview in my project. then, i want this image that in imageview can proccess, rotate. i have code to rotate, but that code done just for image was saved in drawable folder. Please help and save me. in XML any imageview (for showing image), 3 button : "take picture", "open gallery" and "rotate". How image that showing in imageview from gallery and camera can be rotate?
Upvotes: 0
Views: 483
Reputation: 4523
Your code for rotating the image is correct. But I don't understand why are you scaling the image to half.
You'd be better off by removing matrix.postScale(0.5f, 0.5f)
and changing
int newWidth = bMap.getWidth()/2;
int newHeight = bMap.getHeight()/2;
to
int newWidth = bMap.getHeight();
int newHeight = bMap.getWidth();
Upvotes: 1
Reputation: 11
This is my code :
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));
}
} }
This is just work button "Gallery" to showing picture in imageview. But, when i am click "Rotate", the picute just freeze. Please fix my code....
Upvotes: 0