Hello this question might be a little long but i'm going to explain everything for not
getting the same codes i already tried
i got an app inside of it there's a button contents:
try{
Intent Intent1 = new Intent(MainActivity.this , TheSsecondClass2.class);
Intent1.putExtra("bitmapsent", ImageMap); // from user gallery or camera
startActivityForResult(Intent1, 3);
}
then the app makes "in the new activity" imageview equals ImageMap and scales the Imageview using the code:
Captured.setImageBitmap(ImageMap);
Captured.getLayoutParams().height = 1400;
Captured.getLayoutParams().width = 1200;
then the user edits it a little bit then when he finish it uses button that returns the RESULT_OK and it's code:
overlay(Background, currentleft, currentright, currentTop,currentBottom);
// getting things back when the safr"startactivityforresult" starts
Intent IntentBack = new Intent(TheSsecondClass2.this , MainActivity.class);
IntentBack.putExtra("returnedbitmap", bmOverlay);
IntentBack.putExtras(IntentBack);
setResult(RESULT_OK , IntentBack);
finish();
the overlay method returns bitmap bmoverlay is a mixture of bitmaps for editing purposes like editing left,right
bottom,top and the background or the main picture the issue that bitmaps merge to one image and everything is fine but the problem
that when it's back the bitmap is getting bigger and off the edges of the image view even when adding it to gallery i tried to scale it using:
ScaledBit = Bitmap.createScaledBitmap(ImageMap, 1, 1, true);
ScaledBit = ImageMap;
and used -1 or bigger numbers but it's like making it thousand times bigger or so .
and tried the the two codes below:
CapturedImage.setMaxHeight(200);
CapturedImage.setMaxHeight(200);
Bitmap.setwidth();
Bitmap.setHeight();
i might used more but i forgot about it ,all the variables are declared are below :
Bitmap ImageMap;
ImageView CapturedImage;
static Bitmap currentright,currentleft,currentTop,currentBottom;
static Bitmap bmOverlay ,background;
api level 11.
please help and thanks btw.
Related
I am working on an Android application that looks at different properties of images and I noticed that if I start with a JPG image with dimensions 1920x1080 and upload that image into my app as a bitmap by opening the gallery and selecting the appropriate image, the bitmap dimensions are 1024x576. I did not declare a width or height for the bitmap and although I do eventually set my bitmap as an imageView, when I put a break point in my code before this happens the dimensions are still low.
Do I lose resolution in my image?
How can I import images as bitmaps without sacrificing resolution?
Below is my code for importing my image from external storage on the phone into my app
public void openGallery(View v){
runStop = false;
mHandler.removeCallbacks(mStartScan);
mStreamButton.setText("Start stream");
Intent myIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(myIntent, 100);
ppm = 0;
numberOfDots = 0;
if (countParticles.equals(true)){
mDots.setText(null);
mPpm.setText(null);
}
uploadedImage = true;
//listItems.clear();
// mAdapter.notifyDataSetChanged();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 100 && resultCode == RESULT_OK && data!=null){
Uri imageUri = data.getData();
try{
imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
}catch (IOException e)
{
e.printStackTrace();
}
mImageView.setImageBitmap(imageBitmap);
if (processImage.equals(true)){
convertToGray();
}
}
}
Crude comparison of images
Below is a zoomed in comparison of two of the same points. The Android Studio debugger allows bitmaps to be viewed, but not copied or saved so the best I could do is print screen while both images are open on my screen.
To answer your question: No resolution is lost.
I think you were misled by a very confusing display on the Android Studio "View Bitmap" window. It's not displaying the actual size of the bitmap in pixels. I'm not sure exactly what that is (anybody?); maybe it's dp.
It's also clear that the "View Bitmap" image is being downsampled. That's why the pixels look different to you. The preview window is definitely not presenting the image in the same fidelity as it exists in the memory of your Bitmap object.
A better way to check Bitmap's actual dimensions is to look at the mWidth and mHeight members, or use Bitmap.getWidth()/getHeight().
In case you don't believe me, notice that there is a clue in the image you posted, at the very bottom. It's very hard to see, but you can tell that imageBitmap's mBuffer is 8294400 bytes long.
1920 x 1080 x 4 (bytes per pixel) = 8294400
(I tested this on my own box, I was able to reproduce the issue. Trust me -- the actual Bitmap is full size, with no data loss.)
I'm trying to build an simple image-zoom-replace functionality based on the Image-Zoom-Library "PhotoView":
https://github.com/chrisbanes/PhotoView
I just want to load a small image into a imageView and zoom/scale it. This code works perfectly:
ImageView galleryPictureNew = (ImageView)findViewById(R.id.galleryImage);
PhotoViewAttacher mAttacher = new PhotoViewAttacher(view);
mAttacher.update();
The problem is, i want to replace the small image with a large (hd-image) if i zoom in.
There is function called "setOnScaleChangeListener" like this:
mAttacher.setOnScaleChangeListener(new PhotoViewAttacher.OnScaleChangeListener() {
#Override
public void onScaleChange(float scaleFactor, float focusX, float focusY) {
//REPLACE IMAGE
}
});
This code above works, too BUT i want to "jump" to the last "position" and "zooming-level"?!
Does anyone has a solution for it?
Or maybe someone has an alternative library for this problem?
Do i have to save the last "position-state"?
I have a RecyclerView in which I am displaying an integer array of Drawable. However I have a folder of Bitmaps on the sd card I want to display instead. But I have not been able to work out how to convert the bitmaps into drawables and put them into an int array for display or even if this is the best way of working this out.
This is this is the code for where I set my adapter in onCreateView where I use getData() to give the images for the adapter:
adapter = new ProgressImagesListAdapter(getActivity(), getData());
mProgressImagesListRecycler.setAdapter(adapter);
This is getData():
public List<ProgressImagesListInformation> getData() {
List<ProgressImagesListInformation> mFrontViewImagesList = new ArrayList<>();
int[] mImages = new int[]{
R.drawable.boomer,
R.drawable.bosco,
R.drawable.brewster,
R.drawable.dory,
R.drawable.gabby,
R.drawable.hunter,
R.drawable.kitkat,
R.drawable.doug,
R.drawable.mae,
R.drawable.millie
};
for (int i = 0; i < mImages.length; i++)
{
ProgressImagesListInformation current = new ProgressImagesListInformation();
current.mImageId = mImages[i];
mFrontViewImagesList.add(current);
}
return mFrontViewImagesList;
}
The way I was thinking was to change the contents of getdata(), make a for loop that converts the contents of sd card folder bitmaps into array of drawables using somthing like the line below in the for loop but I have not been able to work a way to implement it. This obviously gives me the error found type bitmap expected type integer but I don't understand why or how to fix it.
ArrayList<Integer> ImagesArray = new ArrayList<>();
for (int i = 0; i < sNumberOfFilesInImageDirectory; i++)
{
ImagesArray.add(new BitmapDrawable(getResources(), MyBitmapImage));
}
Thanks for your help.
You'll have to know the location of those images on the SD card. Are they always on the same folder? If so, it'll be easier for you.
First of all, don't forget to declare android.permission.READ_EXTERNAL_STORAGEon your AndroidManifest.xml file.
Then, you'll need to use BitmapFactory along with the path to your images to get the Bitmap objects and put them into a List like an ArrayList or simply use an array.
After that you can use the images like any other. But they won't be int resources, they'll be actual Bitmap objects. To use the resources like R.drawable.someimage they'll have to be located under your res/ folder.
Example:
BitmapFactory.Options opt= new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, opt);
list.add(bitmap); // Or do something else with the Bitmaps.
And if you want to save those images elsewhere, check this answer.
The other steps would require you to understand how to use CardView and RecyclerView. You can read this post on Android Essence and this one on Big Nerd Ranch, and of course this one from the android training guide to get going.
**EDIT: ** And to use a Bitmap on an ImageView, use this:
Bitmap yourBitmap; // Getting from somewhere else.
ImageView myImage = (ImageView) findViewById(R.id.myimageview);
myImage.setImageBitmap(yourBitmap);
And if for some reason you have a Drawable object instead of a Bitmap you can call:
Drawable yourDrawable; // Getting from somewhere else.
ImageView myImage = (ImageView) findViewById(R.id.myimageview);
myImage.setImageDrawable(yourDrawable);
More info on ImageView on this link.
I'm trying to draw on an ImageViewTouch, a library which enables pinch zooming. I'm able to draw over the image using Canvas, but when I zoom the image, the drawing disappears.
For this, I'm trying to convert the view to a bitmap and set theImageBitmap for this same view. Here's the code:
mImage.setDrawPath(true);
mImage.setImageBitmap(loadBitmapFromView(mImage));
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getWidth(), v.getHeight());
v.draw(c);
return b;
}
When I do this, I get the following log error:
07-11 21:13:41.567: E/AndroidRuntime(20056): java.lang.IllegalArgumentException: width and height must be > 0
07-11 21:13:41.567: E/AndroidRuntime(20056): at android.graphics.Bitmap.createBitmap(Bitmap.java:638)
07-11 21:13:41.567: E/AndroidRuntime(20056): at android.graphics.Bitmap.createBitmap(Bitmap.java:620)
If I remove the loadBitmapFromView call the drawing normally appears over the image. When I try to do any interaction with the image (like zooming in or out), the drawing disappears, remaing only the background image, which is a picture.
--- EDIT ---
Here's some more code placed after the loadBitmapFromView call. The case is: I have a radio group listenner and when I check some radio button, I have to load the image and draw some possibles drawings over it.. then I'm trying to convert everything (the image and the drawings) into only one bitmap.
Here's the ohter part of the code:
bitmap = BitmapUtils.decodeSampledBitmapFromResource(root + DefinesAndroid.CAMINHO_SHOPPINGS_SDCARD + nomeImagemAtual, size.x, size.y);
mImage.setImageBitmap(bitmap);
After that, I draw everything I have to draw and try to convert the view to bitmap using the loadImageBitmap method I have shown.
the decodeSampledBitmapFromResource method I got from this link on android developers http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
I finally figured out a solution for my problem.
I'm using the post method of the View, which will be executed only after the view measuring and layouting, this way getWidth() and getHeight() returns the actual width and height.
Here's the code sample:
mImage.post(new Runnable() {
#Override
public void run() {
mImage.setImageBitmap(loadBitmapFromView(mImage));
}
});
Hope it also helps someone else :)
If everything else is correct you are executing your code too early.
The view's layout has not yet been measured by Android. Try executing in OnResume just to see if this is your problem.
Cheers!
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
loadBitmapFromView(mImage);
}
}, 1000);
java.lang.IllegalArgumentException: width and height must be > 0 is because in your Bitmap.createBitmap() call v.getLayoutParams().width or v.getLayoutParams().height is 0. Probably the LayoutParams are wrongly set in mImage.
Update: setLayoutParams() for the mImage before using createBitmap(). Something like this:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
mImage.setLayoutParams(layoutParams);
In my case, I forgot to set orientation to vertical for parent LinearLayout and the default value of horizontal has been set, so I set it and my problem gone...
android:orientation="vertical"
I'm not sure if it relates to this specific question , but I had the same error and I solved it with getting a image source , in the layout xml file , from drawable instead of from mipmap (of course I had to put the image there before).
I have some problems with webview content size. I need to convert full web page to a single image and tried this code
Bitmap screenshot;
screenshot = Bitmap.createBitmap(view.getWidth(), view.getContentHeight(), Bitmap.Config.ARGB_8888);
final Canvas c =new Canvas(screenshot);
view.draw(c);
where "view" is a WebView object
But the result is an image that have 1024x769 pixels. My web page is quite bigger (height is about 2000px). I've tried different ways to solve this problem, but still with zero rezult.
I find solution.
If you have the same problem, you can add onPicture Listener to your webview, like this
web.setPictureListener(new WebView.PictureListener() {
public void onNewPicture(WebView view, Picture picture) {
float temp = (float) view.getHeight();
height = view.getContentHeight() * a;
}
});
and get all your need height and width
For taking the picture of webview you have to enabled cache and from cache you can get it as a bitmap ,below i am mentioning the code hope will useful for you-
webview.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(webview.getDrawingCache());
webview.setDrawingCacheEnabled(false);
it is working for me just check your side,will wait for your acceptance of answer.thanks