Is it possible to get a higher resolution from getDrawingCache? - java

I am using SubsamplingScaleImageView by Dave Morrissey (https://github.com/davemorrissey/subsampling-scale-image-view) to allow users to crop and pan a photo with gestures.
I modified the library to add a tint and a logo to the photo. Now I need to upload the photo to the server. In order to do that I need to somehow extract the photo from the SubsamplingScaleImageView.
I added the following method to the SubsamplingScaleImageView class:
/**
* Capture a photo of the image view
*/
public Bitmap getOutput() {
buildDrawingCache();
Bitmap b1 = getDrawingCache();
Bitmap b = b1.copy(Config.ARGB_8888, true);
destroyDrawingCache();
return b;
}
I am using this method to get the file, resize it to a specific resolution (800x800), and save it to my app's folder.
The problem I noticed is that the extracted photo from the drawing cache depends on the resolution of the device. For example, on my Full HD device I get 1080x1080 photo, which is enough, but on some lower res devices I get resolutions like 480x480 and that is not enough since the image needs to be bigger than that, so the photo gets blurry when I resize it to 800x800.
Is there a way to get the same resolution photo from that image view on all devices?

Related

Getting images to dislay using fromHtml in Android Studio

I am currently using fromHtml in Android Studio to display a webpage's text and images in a TextView. While I am able to load the text just fine, displaying the images doesn't seem to work as expected, leaving small cyan squares in place of where images should be when running the application. After doing some research, I came up with a way I thought would work to display these images as seen below.
content.setText(Html.fromHtml(newContent.content, new Html.ImageGetter() {
#Override public Drawable getDrawable(String source) {
System.out.println(source);
Drawable drawFromPath;
Picasso.get().load(source).into(loadedImage);
drawFromPath = loadedImage.getDrawable();
return drawFromPath;
}
}, null));
Picasso should load the image into a temporary drawable of sorts and return it each time an image is encountered. I know it is finding the images as printing out source prints out the proper image url. However, I can't seem to get it to connect to my app and fill in these cyan squares. Any ideas?

Bitmap and proper saving location

Hi everybody I've got the following problem with an adroid app: I want to read an image saved and perform some operations on it.
(I do not need to use this image to draw something on screen, I just have to check the color of some pixels)
I'm using the following code:
Bitmap bitmap = BitmapFactory.decodeFile("drawable-v24/ean13.bmp");
The fact is that the BitmapFactory returns null as a FileNotFoundException is thrown. (by now the file is saved inside res folder)
I don't really understand where I should put the image and how to give the path to the BitmapFactory to be able to get it properly.
(I guess the problem is due to the fact that the image actually is stored on my pc and not on the Android device but I can't understand how should I proceed)
Thank you in advance for the help! :)
If you are accessing image from resource drawable of mipmap. You should use BitmapFactory.decodeResource to get Bitmap.
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
Or another overloaded version .
public static Bitmap decodeResource(Resources res, int id, Options opts)
where getResources() is method of Activity.

How to take a screen shot with status bar contents in android application?

I have a requirement that I have to take a screenshot of the current window in android.
This is the code am using,
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
View screenView = rootView.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
by using this I could get the application screen shot, but the status bar contents are missing (bluetooth icon, battery status etc).
How can I take a screenshot of the current Android phone window programmatically (including status bar contents)?
Is it possible?
On Android 5.0+, use the media projection APIs to take a screenshot of the device screen. See this sample app, as the process is somewhat involved.
On older Android devices, there is no support for programmatic screenshots of the device screen.

A button which allows an image to be shown in Android Studio

I am building my application using Android Studio, this app can upload an image from raspberry to my emulator. It works fine. What I want to do now is uploading this image and showing it directly to the user without searching it in the gallery. I thought about creating another class and setting this image as a background image in my xml file, but this is too much like I have to create another class every time I want to upload an image from my raspberry.
Can someone help me please. Thank you
If I'm understanding your question correctly, you'd like to load an image from the Android filesystem into your app and display it to the user.
Drawable, Android's generalized image class, allows you to load from file via Drawable#createFromPath.
This SO question suggests Drawable#createFromPath doesn't work on paths beginning with file://, so depending on your use case you may want to precede that with Uri#parse/Uri#getPath.
Once you have a Drawable, you can display it in one of two ways: put an ImageView in your app and call its setImageDrawable method, or set the Drawable as your background image via View#setBackground (note that setBackground was only added in API 16 - in prior versions, you should call View#setBackgroundDrawable).
Putting all of this together, we end up with the following (untested):
private void loadImage(String imagePath) {
Uri imageUri;
String fullImagePath;
Drawable image;
ImageView imageDisplay;
imageUri = Uri.parse(imagePath);
fullImagePath = imageUri.getPath();
image = Drawable.createFromPath(fullImagePath);
imageDisplay = (ImageView) findViewById(R.id.imageDisplay);
/*if image is null after Drawable.createFromPath, this will simply
clear the ImageView's background */
imageDisplay.setImageDrawable(image);
/*if you want the image in the background instead of the foreground,
comment the line above and uncomment this bit instead */
//imageDisplay.setBackground(image);
}
You should be able to modify this to work with any View just by replacing imageDisplay's declared type with the appropriate View type and changing the cast on findViewById. Just make sure you're calling setBackground, not setImageDrawable, for a non-ImageView View.

Getting image from Webview to ImageView

I have a custom WebView. Inside it I've got an image. The WebView is zoomable. I want to reset zoom when I click a button, extract image from there and place inside ImageView.
So far, with the help of x-code I've managed to write this in onClickListener:
while (wv2.zoomOut()){
wv2.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(wv2.getDrawingCache());
bitmapPath = bitmap;
wv2.setDrawingCacheEnabled(false);
new Thread(new Runnable() {
public void run() {
SaveImage(bitmap);
}
}).start();
tstImage.setImageBitmap(bitmap);
}
The WebView zooms out completely, but the Bitmap I get in ImageView is slightly zoomed in. I need to click 1 more time to get the whole bitmap inside the ImageView
I would say that the Javadoc of WebView.getDrawingCache() method explains your problem.
public Bitmap getDrawingCache (boolean autoScale)
...
Note about auto scaling in compatibility mode: When auto scaling is
not enabled, this method will create a bitmap of the same size as this
view. Because this bitmap will be drawn scaled by the parent
ViewGroup, the result on screen might show scaling artifacts. To avoid
such artifacts, you should call this method by setting the auto
scaling to true. Doing so, however, will generate a bitmap of a
different size than the view. This implies that your application must
be able to handle this size.
In your code you simply have to make this change
bitmap = Bitmap.createBitmap(wv2.getDrawingCache(true));

Categories