View vvv=mViewFlipper.getCurrentView();
RelativeLayout rrr=(RelativeLayout)vvv;
ImageView img=(ImageView) rrr.getChildAt(0);
img.setRotation(90);
img.setDrawingCacheEnabled(true);
img.buildDrawingCache();
Bitmap bitmap =img.getDrawingCache();
img.destroyDrawingCache();
img.setDrawingCacheEnabled(false);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);
rrr.removeViewAt(0);
ImageView img_new=new ImageView(ImageSlideShow.this);
img_new.setImageDrawable(bmd);
img_new.setScaleType(ScaleType.CENTER);
rrr.addView(img_new, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
I have Used Above code for Image rotating in ViewFlipper.
First time it is executed but second time it return NullPointerException...Error line:
Bitmap bitmap =Bitmap.createBitmap(img.getDrawingCache());
hi use this code and hopefully this works
basically your image view is null so first inintiallise it
ImageView img= new ImageView();
img=(ImageView) rrr.getChildAt(0);
img.setRotation(90);
img.setDrawingCacheEnabled(true);
img.buildDrawingCache();
if(img.isDrawingCacheEnabled()){
Bitmap bitmap =img.getDrawingCache(true);
}img.destroyDrawingCache();
img.setDrawingCacheEnabled(false);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,
bao);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);
rrr.removeViewAt(0);
ImageView img_new=new ImageView(ImageSlideShow.this);
img_new.setImageDrawable(bmd);
img_new.setScaleType(ScaleType.CENTER);
rrr.addView(img_new, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
I got Solution For this problem
ImageView img=(ImageView) rrr.getChildAt(0);
Matrix matrix = new Matrix();
matrix.set(img.getImageMatrix());
matrix.postRotate(90, img.getWidth() / 2, img.getHeight() / 2);
img.setImageMatrix(matrix);
above code complete working for image rotation and you must set android:scaleType="matrix" in your imageview.
Related
I have found a lot of solutoins how to rotate image relatively to point like whis.
But I need rotate image relatively to x axis, like this.
Any help appreciate.
try this,
ImageView imageView;
Matrix mMatrix = new Matrix();
imageView = (ImageView) findViewById(R.id.myimage);
Bitmap bmp = ((BitmapDrawable) getResources().getDrawable(
R.drawable.icon_150_380882090)).getBitmap();
Camera camera = new Camera();
camera.save();
camera.rotateY(50f);
//camera.rotateX(50f);
//camera.rotateZ(50f);
camera.getMatrix(mMatrix);
camera.restore();
Bitmap bm = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
bmp.getHeight(), mMatrix, true);
imageView.setImageBitmap(bm);
I have checked many discussion but i can't seem to find an answer. How can i crop and large image taken by a camera and crop it to a 640x640 pixel size? Im returning a URI
EDIT: I would like to allow the user to crop the image!
Another solution would be to use the createScaledBitmap people use to create thumbnails.
byte[] imageData = null;
try
{
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(fileName);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
}
catch(Exception ex) {
}
Your bitmap imageBitmap would probably have to come directly from your camera instead of a file, but the general idea stays the same.
You may use
private Bitmap crop(Bitmap src, int x, int y, int width, int height) {
Bitmap dst = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(dst);
canvas.drawBitmap(src, new Rect(0, 0, src.getWidth(), src.getHeight()),
new Rect(x, y, width, height), null);
return dst;
}
Type arguments are self explanatory.
Good luck.
Try this code, using the intent object:
intent.setType("image/*");
intent.putExtra("outputX", int_Height_crop);
intent.putExtra("outputY", int_Width_crop);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
use the below code
You can use this link also for your reference
Click Crop image using rectengle!
int targetWidth = 640;
int targetHeight = 640;
Bitmap targetBitmap = Bitmap.createBitmap(
targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addRect(rectf, Path.Direction.CW);
canvas.clipPath(path);
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), null);
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
imageView.setImageBitmap(targetBitmap);
I need to draw a bitmap that is resized on another bitmap (scale down one bitmap and then draw it on another).
The code I am using right now is:
Resources res = ((Context)(activity)).getResources();
Drawable[] layers = new Drawable[2];
layers[0] = res.getDrawable(R.drawable.marker_report_red);
layers[1] = new BitmapDrawable(getResizedBitmap(((BitmapDrawable)res.getDrawable(R.drawable.report_path_merge_ic)).getBitmap(),10,10));
LayerDrawable layerDrawable = new LayerDrawable(layers);
Bitmap b = Bitmap.createBitmap(layers[0].getIntrinsicWidth(), layers[0].getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
layerDrawable.setBounds(0, 0, layers[0].getIntrinsicWidth(), layers[0].getIntrinsicHeight());
layerDrawable.draw(new Canvas(b));
and:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
But the resulting image just shows a fuzzy bitmap that is stretched to the size of the underlying one.
How can I do this? This is the 4th method I am trying...
I fixed the problem, I am using a different code completely:
Resources res = ((Context)(activity)).getResources();
Drawable[] layers = new Drawable[2];
layers[0] = res.getDrawable(R.drawable.marker_report_red);
layers[1] = res.getDrawable(R.drawable.report_path_merge_ic);
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(1, 15, 10, 15, 15);
Bitmap b = Bitmap.createBitmap(layers[0].getIntrinsicWidth(), layers[0].getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
layerDrawable.setBounds(0, 0, layers[0].getIntrinsicWidth(), layers[0].getIntrinsicHeight());
layerDrawable.draw(new Canvas(b));
With emphasis on the line:
layerDrawable.setLayerInset(1, 15, 10, 15, 15);
This line resized the bitmap on the second layer.
I am trying to translate and rotate a bitmap in android ImageView. But I can't get the result. Any pointers will be helpful.
int width = mb.getWidth();
int height = mb.getHeight();
// create a matrix for the manipulation
Matrix matrix = new Matrix();
matrix.preTranslate(width/2, height/2)
matrix.postRotate(angle);
Bitmap resizedBitmap = Bitmap.createBitmap(mb, 0, 0,
width, height, matrix, true);
imageview.setImageBitmap(resizedBitmap);
How do you convert Matrix to Bitmap/Drawable?
Actually I do not understand your question. But if you want to resize a image then this might be helpful for you:
Bitmap bmap = BitmapFactory.decodeStream(getClass().getResourceAsStream("Your image path"));
int oldHeight= ..
int newHeight = ..
int oldWidth = ..
int newWidth = ..
scaleheight= newHeight/oldHeight
scaleWidth = newWidth/oldWidth
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap and set it back
Bitmap resizedBatteryImage = Bitmap.createBitmap(bmap, 0, 0,oldWidth, oldHeight, matrix, true);
try this code
ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
Matrix mat = new Matrix();
Bitmap bMapimage = Bitmap.createBitmap(bMap, 0, 0, bMap.getWidth(), bMap.getHeight(), mat, true);
the result of bMapimage have your bitmap image. And one more thing ,remember about the memory management while using bitmap,bitmapfactory...After using this try to set it as null.