android change picture taken size - android

My app uses the camera to take pictures and sends the data someplace else. However, the picture sizes are too big in terms of bytes, or unecessarily big. But I'm not sure how to force the camera to take a smaller picture or after taking the picture, send a scaled down version of it.
This is how I go to the Camera screen.
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(getTempFile()));
startActivityForResult(intent, PIC_ONE);//first picture
And then onActivityResult I have:
...
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=4;
Bitmap mBitmap = BitmapFactory.decodeFile(getPath(myURI),options);
photoView.setImageBitmap(bmp);
Which shows the user a quarter sized thumbnail of the saved image. But of course the actual image is still retains its large size.
How can I reduce the image size?

Have a look at Reduce Bitmap size using BitmapFactory.Options.inSampleSize
You can also try createScaledBitmap()
public static Bitmap createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter)
Since: API Level 1
Creates a new bitmap, scaled from an existing bitmap.
Parameters
src The source bitmap.
dstWidth The new bitmap's desired width.
dstHeight The new bitmap's desired height.
filter true if the source should be filtered.
Returns the new scaled bitmap.
It also reduces the file size,
You can also use Following function for resizing:
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;
}

Related

How to resize image without loosing image content

I want to resize an image to a smaller image like thumbnail. I have used following options
Bitmap imageBitmap = Bitmap.createScaledBitmap(mBitmap, 200, 700, false);
and
ThumbnailUtils.extractThumbnail()
Both of these options cut parts of images. So they don't look good. I just want to reduce image dimensions and don't want to loose image content.
you have to try like this
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;
}
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(filepath,bmOptions);
//Part 1 :withcompression Sacle image 200 pixels x 700 pixels this size you can customize as per your requirement
Bitmap withCompressed = Bitmap.createScaledBitmap(bitmap,200,700,true);
If you want to resize an image without losing quality and images are from drawable/mipmap then you should try pngquant.
https://pngquant.org/

How to pixelize a bitmap in android

I have been searching but i did not find a question about how to pixelize a bitmap in android
Example
Note that i dont mean blur
You could just try and resize that image to a smaller version (and then back up if you need it at the same size as the original for some reason)
{
Bitmap original = ...;
//Calculate proportional size, or make the method accept just a factor of scale.
Bitmap small = getResigetResizedBitmap(original, smallWidth, smallHeight);
Bitmap pixelated = getResigetResizedBitmap(small, normalWidth, normalHeight);
//Recycle small, recycle original if no longer needed.
}
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
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;
}
The code is from here
The simplest way to pixelate the image would be to scale image down using "nearest neighbour" algorithm, and then scale up, using the same algorithm.
Filtering over the image trying to find an average takes much more time, but does not actually give any improvements in result quality, after all you do intentionally want your image distorted.

Fast load image from sdcard

I want that load images from sdcard to listview but i dont need image in big size. size 60dip * 60dip is Sufficient. i am using below codes in thread but however this is slow.i need load images in fast way.
public Bitmap loadImage(String imagePath){
//load bitmap in real size
Bitmap bmp = BitmapFactory.decodeFile(imagePath);
int width = bmp.getWidth();
int height = bmp.getHeight();
//determine size and scale
float newWidth = convertDipToPixel(60, getApplicationContext());//60 dip
float scale = ((float) newWidth) / width;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scale, scale);
Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
return resizedBitmap;}
check android query and image loading using android query
its really fast, try it.
File file = new File(path);
//load image from file, down sample to target width of 300 pixels
aq.id(R.id.avatar).image(file, 300);
you can also use Android-Universal-Image-Loader
String imageUri = "file:///mnt/sdcard/image.png"; // from SD card
// Load image, decode it to Bitmap and display Bitmap in ImageView (or any other view
// which implements ImageAware interface)
imageLoader.displayImage(imageUri, imageView);

how to resize the image and its quality not be change in android?

I am creating an application and want to setup Image . I do not want the images in same size, but same image quality. how to resize that image but same image quality in android ?
i'm using this method to resize the image
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
int h = myBitmap.getHeight() / 20;
int w = myBitmap.getWidth() / 20;
Bitmap scaled = Bitmap.createScaledBitmap(myBitmap, w, h,
true);
imageTab.setImageBitmap(scaled);
Pass btimap you want to resize and size in which you want it to resize in following method
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;
}
Your requirement makes no sense. Whenever you resize it the quality will be changed. Downscaling will make your image lose image data like thin lines etc, and upscaling will make your image blurred and lose clarity.
This is because you only have so much image data with you, and you cannot arbitrarily change the amount of data you have to produce good quality images.
That said, using a format like PNG over JPG can help you save some of the quality at least, as JPG compresses the data even more.
Here is code snippet ..
private static BufferedImage resizeImage(BufferedImage originalImage, int type){
BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
Graphics2D g = resizedImage.createGraphics();
g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
g.dispose();
return resizedImage;
}
BufferedImage resizeImagePng = resizeImage(originalImage, type);
ImageIO.write(resizeImagePng, "png", new File("Image PAth"));

How to make an image small rather than its original size?

I am developing an animation app in the android.
i have one big image in application.
The image should start from the center of screen. The image size will be bigger at the initial stage. While moving to the left of the screen, its size should decrease(i.e. scaling should take place). Image should not go back to its original position. It should be placed at the leftside of the screen itself after the animation.
Can anyone please help.
Thanking you.
If I understand conrrectly you want to display an image and then animate it so that it appers to be reducing in size giving an affect that is going into the background.
You need to use ScaleAnimation
http://developer.android.com/reference/android/view/animation/ScaleAnimation.html
A good tutorial regarding android animation can be found at:
http://www.hascode.com/2010/09/playing-around-with-the-android-animation-framework/#Scale_Animations
this function resize the bitmap
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);
matrix.postRotate(90);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
matrix, false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 70, bytes);
return resizedBitmap;
}

Categories

Resources