how to get runtime width and height of a compundDrawable - java

I want to know the runtime width and height of a compoundDrawble.
I have tried to use layout inspector, but it shows the height of the text box + drawable.
ViewObserverTree won't help to separate the frawble and the text
to get only the drawable height.
I have also tried drawable.getBounds()
but it will give me the compile time height and width.
Drawable[] compoundDrawablesRelative = TextViewCompat
.getCompoundDrawablesRelative(targetTextView);
drawable.getBounds().
How would you get the runtime values (in pd and pxl)

Drawable[] drawables = textView.getCompoundDrawables();
Bitmap myBitmap = ((BitmapDrawable)drawables[1] ).getBitmap();
//Bitmap myBitmap = ((BitmapDrawable )imageView.getDrawable()).getBitmap();
int width = myBitmap .getWidth();
int height = myBitmap .getHeight();
try this code

Related

How to check if Image Quality is Low

I am developing a PhotoBook app where I need to notify the user if they add a low-resolution image. I just need to show a "Low-Resolution Image, Printing may be affected" warning as chatbooks app do.
int file_size = Integer.parseInt(String.valueOf(file.length() / 1024));
if (file_size < 100){
Log.v(TAG, "Low resolution image");
}else{
Log.v(TAG, "");
}
you can convert image into bitmap and check its height and width and with this its help you to check the image resolution.
if image is from drawable,
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.back);
bmp.getWidth(), bmp.getHeight();
if image from uri
Uri imageUri = data.getData();
Bitmap bitmap =
MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
bitmap.getWidth(), bmp.getHeight();
make a validation if image height and width less than your need than show your alert
If you have a Bitmap object, you can check for bitmap.getWidth() and getHeigth()
You can check the width and height of the image and thus you can decide which resolution is appropriate to classify it as low quality.
public void checkQuality() {
String filePathTmp = new File("").getAbsolutePath();//getCanonicalPath();
//notice you must use you image path
Path filePath = Paths.get(filePathTmp, "\\YOUR\\PARTH\\IMAGE.png").normalize();
ImageIcon imageIcon = new ImageIcon(filePath.toString());
int height = imageIcon.getIconHeight();
int width = imageIcon.getIconWidth();
System.out.println("HEIGHT: " + height + "--" + "WIDTH" + width);
//change values, consider your own criteria low quality
if (height <100 || width <100){
System.out.println("Low quality");
}
}

pdf is not scaling in pdfview android

I am using library Dmitry-Borodin/pdfview-android for showing pdf in android. My view is showing pdf correctly but the scaling of pdf doesnot seems to be working.
pdfView!!.fromFile(file!!).scale(500f).show()
Above is my code for showing pdf and scaling it.
With scaling I mean that width of pdf is quite small than that of device screen width
Instead of using Library, Please go through ImageView Component.
You need to create a Bitmap that matches the aspect ratio of the Page. It's best to match the dimensions of the ImageView as well:
PdfRenderer renderer = new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
PdfRenderer.Page page = renderer.openPage(0);
int pageWidth = page.getWidth();
int pageHeight = page.getHeight();
float scale = Math.min((float) REQ_WIDTH / pageWidth, (float) REQ_HEIGHT / pageHeight);
Bitmap bitmap = Bitmap.createBitmap((int) (pageWidth * scale), (int) (pageHeight * scale), Bitmap.Config.ARGB_8888);
page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
imageView.setImageBitmap(bitmap);

How to convert the view into bitmap without adding into the UI in Android

I am using View in android and I need to convert this to Bitmap without adding this into the activity.
view.setDrawingCacheEnabled(true);
Bitmap bitmap= view.getDrawingCache();
It returns bitmap as null.
Also I have tried the method view.buildDrawingCache() but still getDrawingCache() return null.
Thanks in advance.
First you need to create empty bitmap and get canvas from it.
use below code for this
int w = WIDTH_PX, h = HEIGHT_PX;
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
Canvas canvas = new Canvas(bmp);
Now you need to draw you view in this bitmap.
use below code for this
LinearLayout layout = new LinearLayout(getContext());
TextView textView = new TextView(getContext());
int padding = 4;
textView.setPadding(padding, padding, padding, padding);
textView.setVisibility(View.VISIBLE);
textView.setText("Hello how are you");
layout.addView(textView);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
layout.measure(canvas.getWidth(), canvas.getHeight());
layout.layout(0, 0, canvas.getWidth(), canvas.getHeight());
layout.draw(canvas);
now your view is converted into the bitmap (bmp).

Get bitmap width and height is varing in same bitmap

In my application, I need to find out exact dimensions of a bitmap in pixels. The real dimension of the bitmap is 600x583. When I call
mBitmap.getWidth();
mBitmap.getHeight();
I get 300x292 as the dimensions of the bitmap.
After I again call that same bitmap with
mBitmap.getWidth();
mBitmap.getHeight();
Then I get 240x233. Everytime the ratio is same. But it's not the correct value. How can I overcome this issue?
mBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.birdsketchfinal2).copy(Config.ARGB_8888, true);
height=mBitmap.getHeight();
width=mBitmap.getWidth();
//In here I get 300X292
disWidth = getResources().getDisplayMetrics().widthPixels;
disHeight = getResources().getDisplayMetrics().heightPixels;
modified=((float)disWidth/(float)width)*height;
mBitmap= Bitmap.createScaledBitmap(mBitmap, disWidth, (int)modified, true);
height=mBitmap.getHeight();
width=mBitmap.getWidth();
//I here I get 240x233

Android, Java, Creating a thumbnail keeping aspect ratio

I'm trying to create a thumbnail of a certain height but maintain the aspect ratio. I'm using the code below but the problem arises when say if an image is somewhat small the image generated will not fill the thumbnail area. imageURI is just the path to the image.
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imageURI, o);
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=4;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale++;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Bitmap bitmap = BitmapFactory.decodeFile(imageURI, o2);
Your code is always scaling the bitmap to have at least 1/4 of width or heigth. If the original image is already large enough it will be even smaller.
I assume you display the image in an ImageView (your thumbnail area?. If the image does not fill the ImageView you have to configure the ImageView to resize the image correctly. If your ImageView and the image to display have different aspect ratios, the only way to make the image fill the ImageView will distort the image.
What I do: I use the BitmapFactory to decode the image into a size thats larger, but nearly the size I want the thumbnail to have. Its better to use powers of two as scaling parameter, so I do that. And then I set the android:scaleType parameter of ImageView to make the image display as I like:
public static Bitmap decodeBitmap(Uri bitmapUri, ContentResolver resolver, int width, int height) throws IOException{
InputStream is = resolver.openInputStream(bitmapUri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is,null,options);
is.close();
int ratio = Math.min(options.outWidth/width, options.outHeight/height);
int sampleSize = Integer.highestOneBit((int)Math.floor(ratio));
if(sampleSize == 0){
sampleSize = 1;
}
Log.d(RSBLBitmapFactory.class, "Sample Size: " + sampleSize);
options = new BitmapFactory.Options();
options.inSampleSize = sampleSize;
is = resolver.openInputStream(bitmapUri);
Bitmap b = BitmapFactory.decodeStream(is,null,options);
is.close();
return b;
}
<ImageView android:scaleType="fitXY"></ImageView>

Categories