Trying to capture an imageView then put it in a bmp - android

I'm trying to capture an imageView and out it in a bmp. It currently works but, i am not able to make the captured imageView the correct size. This gives me a problem was black bar on the side of my imageView when it put the drawingcache as a bmp back into the imageview for display.
My code is like this:
imageView.setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(imageView.getDrawingCache());
I tired doing :
Bitmap bm = Bitmap.createBitmap((int) dw, (int) dh,imageView.getDrawingCache());
but imageView.getDrawCache (BMP type) is not same type as Bitmap.Config.ARGB_8888. I'm not sure how to tackle this specific problem. Any help would be nice.
Please read this question for more detail and screenshots.
Black bars on the side of screen capture on imageView

Related

Get bitmap from imageview with vectordrawable resource

I want to get bitmap from imageview with vectordrawable resource that rotated on X or Y axis but the quality of bitmap is not good. The quality without rotation or "setRotation" is good but when I use "setRotationX" or "setRotationY" or Camera to rotate, the bitmap quality comes down. This problem is just when I set "setImageResource" to a vectordrawable, jpeg or png images have not this problem. I think this is a bug on vectordrawable...
This is my code to get a bitmap:
itemBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(itemBitmap);
cv.save();
imageView.draw(canvas);
cv.restore();
If you're trying to get a bitmap from an imageView, the quality of the bitmap is more likely to be degraded. If the image is rotated it will have even less quality as the bitmap needs to be zoomed out.
You can try following things:
convert the vector resource directly into bitmap and rotate using matrix or some libraries. or,
turn off hardware acceleration. or,
Use imageView.setDrawingCacheEnabled(false); followed by Bitmap bitmap = imageView.getDrawingCache();
Pl refer to kc Ochibili's answer for question at following address:
How to Get Pixel Colour in Android?
I am using same on my VectorDrawable like (what I have shown below). I am giving code details like a narration because I am posting this from my mobile and there is problem with indentation which is not accepted by the system at stackoverflow.
First attach a listener to the ImageView like
myImageView.setOnTouchListener(new View.OnTouchListener(){
Then override the following method:
Public boolean onTouch(View view, MotionEvent motionEvent){
Then from the view parameter received take IageView
ImageView img = (ImageView) view;
Then on the image view do the following:
Img.setDrawingCacheEnablef(true);
Then take bitmap from that
Bitmap imgbmp = Bitmap.createBitmap(img.getDrawingCache());
Immediately setDrawingCacheEnabled(false) on img.
You have the bitmap.

How to get the image bitmap of an ImageButton?

I have no problem doing:
currentImageButton.setImageBitmap(bitmap);
However, I have yet to be able to get the image bitmap back. I am trying to get the image bitmap BACK from the ImageButton and use it to set the background of my canvas.
currentImageButton.buildDrawingCache();
Drawable myDrawable = currentImageButton.getDrawable();
Bitmap anImage = ((BitmapDrawable) myDrawable).getBitmap();
canvas.drawBitmap(getResizedBitmap(anImage, 1000, 1000));
None of this seems to work though. I've even tried using myDrawable to set change the background of my view, just incase it was something wrong with canvas but that doesn't work either.
Bitmap bitmap = ((BitmapDrawable)imageBitmapInstance.getDrawable()).getBitmap();

How to screen shot a ImageView without lost quality

I have two ImageView's in a RelativeLayout, the first one is as large as the RelativeLayout size (as background),and the second ImageView has a small image, now I want to combine these to ImageView's image into one by screenshot. not by combine two bitmap directly.
In the snippet below, I successfully take screenshot of the RelativeLayout, but I found the image quality is not as good as the first ImageView's bitmap, can anyone help me please?
view.setDrawingCacheEnabled(true);
view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap drawCache = view.getDrawingCache(true);
Try to get bitmap using window DecorView :
View window = activity.getWindow().getDecorView()
Canvas bitmapCanvas = new Canvas();
Bitmap bitmap = Bitmap.createBitmap(window.getWidth()*2, window.getHeight()*2, Bitmap.Config.ARGB_8888);
bitmapCanvas.setBitmap(bitmap);
bitmapCanvas.scale(2.0f, 2.0f);
window.draw(bitmapCanvas);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, myOutputStream);
Ref : High resolution screen shot in Android

how to crop top,left,right and bottom portion of imageview in android

In my application i am getting transparent pixels in my Imageview while taking screenshot.How to cut top,left,right and bottom portion of Imageview.I tried below code but not working
Bitmap bitmap = Bitmap.createBitmap(imgView.getWidth(),imgView.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
imgView.draw(canvas);
Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100);
bitmap.recycle();
please help me to solve this issue.
I am not very sure what are you referring with "not working", but from first sight I would suggest you to change from
Bitmap result =Bitmap.createBitmap(bitmap,imgView.getLeft()+10, imgView.getTop()+50, imgView.getWidth()-20, imgView.getHeight()-100);
to
Bitmap result =Bitmap.createBitmap(bitmap, 10, 50, bitmap.getWidth()-20, bitmap.getHeight()-100);
But I would not recommend you to use this kind of approach which explicitly setting width and height value.

Crop particular part of image in android

I want to crop Red part from following image, Is there any simple method available in android that can crop following image.
I have found many SO questions but all are suggesting to used following code:
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100);
This code work well if width & height are around 2MP resolution, but if that cropped part is more than 3MP resolution than application got crashed with OOM error.
Is there any way that handle image more than 3MP during cropping?
You can used following code that can solve your problem.
Matrix matrix = new Matrix();
matrix.postScale(0.5f, 0.5f);
Bitmap croppedBitmap = Bitmap.createBitmap(bitmapOriginal, 100, 100,100, 100, matrix, true);
Above method do postScalling of image before cropping, so you can get best result with cropped image without getting OOM error.
For more detail you can refer this blog
1- Change your imageview for bitmap
final Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.img);
2-use your bitmap to crop what you want
Bitmap croppedBmp = Bitmap.createBitmap(bitmap, x, y , width , height);
3-Take care x,y from Top and left
4- to preview your bitmap again in your imageview
imageView.setImageBitmap(croppedBmp);
If you want to crop image in any shape OR only selected part then
you can use ready made open source library

Categories

Resources