I am trying to get a big size image programmatically , but app is crashing.
Here is what I'm doing:
public static Bitmap getImageFromResult(Context context, int resultCode, Intent imageReturnedIntent) {
Bitmap bitmap = null;
File imageFile = getTempFile(context);
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage;
boolean isCamera = (imageReturnedIntent == null || imageReturnedIntent.getData() == null || imageReturnedIntent.getData().toString().contains(imageFile.toString()));
if (isCamera) {
// From Camera
selectedImage = Uri.fromFile(imageFile);
} else {
// From Storage
selectedImage = imageReturnedIntent.getData();
}
//bitmap = getResizedImage(context, selectedImage);
bitmap = getResizedImage(context, selectedImage);
}
return bitmap;
}
And the resizemethod` :
//*********** Resize to avoid using too much Memory loading Big Images (2560*1920) ********//
private static Bitmap getResizedImage(Context context, Uri selectedImage) {
Bitmap resizedBitmap = null;
int[] sampleSizes = new int[]{8,7,6,5, 4, 3, 2, 1};
int i = 0;
do {
resizedBitmap = decodeBitmap(context, selectedImage, sampleSizes[i]);
i++;
} while (resizedBitmap.getWidth() < minWidthQuality && i < sampleSizes.length);
return resizedBitmap;
}
Try this - you can resize the Bitmap to another height/width
public static Bitmap getResizedBitmap(Bitmap bm, float newHeight, float newWidth) {
float width = bm.getWidth();
float height = bm.getHeight();
float scaleWidth = (newWidth) / width;
float scaleHeight = (newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(bm, 0, 0, (int) width, (int) height, matrix, false);
}
Then just make your new Bitmap
Bitmap bitmap = BitmapFactory.decodeFile(pathname);
Bitmap newBitmap = getResizedBitmap(bitmap,400,320);
If you are getting image live from internet then obviously you need to first download the image but it will take time according to internet speed or you want to load image from memory then you can apply this stack over answer
method here to get decrease the image quality
Related
My bitmap is rotated but is overlaped with original image.
Bitmap source
public void onImageAvailable(ImageReader reader) {
final Image image = reader.acquireNextImage();
// final Image image = reader.acquireLatestImage();
final ByteBuffer yuvBytes = imageToByteBuffer(image);
// Convert YUV to RGB
final RenderScript rs = RenderScript.create(MainActivity.this);
Bitmap bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(), Bitmap.Config.ARGB_8888);
final Allocation allocationRgb = Allocation.createFromBitmap(rs, bitmap);
final Allocation allocationYuv = Allocation.createSized(rs, Element.U8(rs), yuvBytes.array().length);
allocationYuv.copyFrom(yuvBytes.array());
ScriptIntrinsicYuvToRGB scriptYuvToRgb = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
scriptYuvToRgb.setInput(allocationYuv);
scriptYuvToRgb.forEach(allocationRgb);
allocationRgb.copyTo(bitmap);
int rotation = MainActivity.this.getWindowManager().getDefaultDisplay().getRotation();
int x = bitmap.getWidth();
int y = bitmap.getHeight();
Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap, y, x, true);
Bitmap rotatedBitmap = rotateBitmap(bitmap1, getOrientation(rotation));
RotatedBitmapFunction
public Bitmap rotateBitmap(Bitmap original, float degrees) {
int width = original.getWidth();
int height = original.getHeight();
Matrix matrix = new Matrix();
matrix.preRotate(degrees);
Bitmap rotatedBitmap = Bitmap.createBitmap(original, 0, 0, width, height, matrix, true);
Canvas canvas = new Canvas(rotatedBitmap);
canvas.drawBitmap(original, 5.0f, 0.0f, null);
return rotatedBitmap;
}
Output
The original bitmap is stacked on top of rotatedbitmap.
#Problem
Check these lines in your code
Canvas canvas = new Canvas(rotatedBitmap);
canvas.drawBitmap(original, 5.0f, 0.0f, null);
Why you are drawing the origin bitmap in the canvas just before returning the rotated Bitmap.
I guess this is the issue.
Solved It
public Bitmap rotateBitmap(Bitmap original, float degrees) {
int x = original.getWidth();
int y = original.getHeight();
Matrix matrix = new Matrix();
matrix.preRotate(degrees);
Bitmap rotatedBitmap = Bitmap.createBitmap(original , 0, 0, original .getWidth(), original .getHeight(), matrix, true);
return rotatedBitmap;
}
Hi everyone and beforehand thanks, I hope are well write because I am doing a simple application that should unite more than two bitmaps in only one, the problem is in which position bitmaps and size Side wrong, and the truth will not find the back for logic given to me that 's fine, in fact it is a Tengo Que code already in c # and PASE java obviously is different sin but have the same principle .
I wonder if you have the way to make the position and size of these images out as this saying in the code,
Sorry for my bad English
CODIGO JAVA
Paint mPaint;
Bitmap image1=BitmapUtils.decodeBase64(Lie.GeFondo().GetImagen());
Bitmap image2=BitmapUtils.decodeBase64(Utilidades.getImagenTomadabase64());
Bitmap image3=BitmapUtils.decodeBase64(Lie.GetBanner().GetImagen());
Bitmap result = Bitmap.createBitmap(image1.getWidth(), image1.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Rect srcRect = new Rect(0, 0, image1.getWidth(), image1.getHeight());
Rect dstRect = new Rect(srcRect);
Rect srcRect1 = new Rect( Foto.GetPosicionDeItems().Getx(),Foto.GetPosicionDeItems().Gety(),Foto.GetTamano().GetWidth(), Foto.GetTamano().GeHeight());
Rect srcRect3 = new Rect( Lie.GetBanner().GetPosicionDeItems().Getx(), Lie.GetBanner().GetPosicionDeItems().Gety() ,Lie.GetBanner().GetTamano().GetWidth(), Lie.GetBanner().GetTamano().GeHeight());
Rect srcRect2 = new Rect(0,0,image2.getWidth(), image2.getHeight());
Rect srcRect4 = new Rect(0,0,image3.getWidth(), image3.getHeight());
dstRect.offset(0, 0);
canvas.drawBitmap(image1, srcRect, dstRect, null);
dstRect.offset(image1.getWidth(), 0);
srcRect1.offset(0, 0);
canvas.drawBitmap(image2,srcRect2 ,srcRect1 , null);
srcRect1.offset(image2.getWidth(), 0);
srcRect3.offset(0, 0);
canvas.drawBitmap(image3,srcRect4 ,srcRect3 , null);
srcRect3.offset(image3.getWidth(), 0);
myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(result);
in Java
see java picture http://i58.tinypic.com/1zywm5u.jpg
C# Code
Ignore the foreach.
System.Drawing.Bitmap Bac = (System.Drawing.Bitmap)Lienzo.Fondo.Imagen;
System.Drawing.Graphics r = System.Drawing.Graphics.FromImage(Bac);
if (Lienzo.Fotos != null)
{
if (Lienzo.Fotos.Count > 0)
{
int i = 0;
foreach (RADMLIB.Items item in Lienzo.Fotos)
{
System.Drawing.Bitmap img = (System.Drawing.Bitmap)Lista[i];
r.DrawImage(img, item.PosicionDeItems.X, item.PosicionDeItems.Y, item.Tamano.Width, item.Tamano.Height);
i++;
}
}
}
if (Lienzo.Banner != null)
{
r.DrawImage((System.Drawing.Bitmap)Lienzo.Banner.Imagen, Lienzo.Banner.PosicionDeItems.X, Lienzo.Banner.PosicionDeItems.Y, Lienzo.Banner.Tamano.Width, Lienzo.Banner.Tamano.Height);
}
return Bac;
see c# picture http://i61.tinypic.com/s61wlh.jpg
I found the solution
using Matrix for set location and scale x,y
Bitmap image1=BitmapUtils.decodeBase64(Lie.GeFondo().GetImagen());
Bitmap image2=BitmapUtils.getResizedBitmap(BitmapUtils.decodeBase64(Utilidades.getImagenTomadabase64()),Foto.GetTamano().GetWidth(),Foto.GetTamano().GeHeight());
Bitmap image3=BitmapUtils.getResizedBitmap(BitmapUtils.decodeBase64(Lie.GetBanner().GetImagen()),Lie.GetBanner().GetTamano().GetWidth(),Lie.GetBanner().GetTamano().GeHeight());
Bitmap result = Bitmap.createBitmap(image1.getWidth(), image1.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);//Create the canvas to your image
Rect srcRect = new Rect(0, 0, image1.getWidth(), image1.getHeight());
Rect dstRect = new Rect(srcRect);
Matrix matrix = new Matrix ();
Matrix matrix2 = new Matrix ();
matrix.postTranslate( Foto.GetPosicionDeItems().Getx(),Foto.GetPosicionDeItems().Gety());
matrix2.postTranslate( Lie.GetBanner().GetPosicionDeItems().Getx(),Lie.GetBanner().GetPosicionDeItems().Gety());
canvas.drawBitmap(image1, srcRect, dstRect, null);
dstRect.offset(image1.getWidth(), 0);
canvas.drawBitmap(image2,matrix , null);
canvas.drawBitmap(image3,matrix2 , null);
getResizedBitmap Method
public static 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;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
see the image
https://lh4.ggpht.com/LXW8kVc3U8qQUHnORI-3H4H-A2hjq92y_oEDsKIs-iBDkVBFTgjGP03xFReCeuyLlg=h900-rw
I have some different size bitmap here:
and I want to resize them to same width and height (64px) each image
here is what I did:
int size = Math.round(64 * getResources().getDisplayMetrics().density);
int imagesLength = 4;
Bitmap bmp = Bitmap.createBitmap(size*imagesLength, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
for (int i = 0; i < imagesLength; i++) {
Bitmap mImage = getImage(i);
// ...code to get bitmap...
canvas.drawBitmap(mImage,i * size, 0, null);
}
do anyone got an idea? thanks
You can use Bitmap.createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter) to change the image to the size you want.
But this will not keep the aspect ratio intact. If thats important you might want to think of something else. Like scaling it down as you already did and adding it centered on some kind of background.
This gets you square bitmaps, no matter what:
int size = Math.round(64 * getResources().getDisplayMetrics().density);
int imagesLength = 4;
Bitmap bmp = Bitmap.createBitmap(size*imagesLength, size, Bitmap.Config.ARGB_8888); // the bitmap you paint to
Canvas canvas = new Canvas(bmp);
for (int i = 0; i < imagesLength; i++) {
Bitmap mImage = getImage(i);
// ...code to get bitmap...
mImage = Bitmap.createScaledBitmap(mImage , size, size, true);
canvas.drawBitmap(mImage,i * size, 0, null);
}
And this is for keeping the ratio but leaving gaps:
int size = Math.round(64 * getResources().getDisplayMetrics().density);
int imagesLength = 4;
Bitmap bmp = Bitmap.createBitmap(size*imagesLength, size, Bitmap.Config.ARGB_8888); // the bitmap you paint to
Canvas canvas = new Canvas(bmp);
for (int i = 0; i < imagesLength; i++) {
Bitmap mImage = getImage(i);
// ...code to get bitmap...
int width = mImage.getWidth();
int height= mImage.getHeight();
float ratio = width/(float)height;
if(ratio>1)
{
mImage = Bitmap.createScaledBitmap(mImage , size, size/ratio, true);
}
else
{
mImage = Bitmap.createScaledBitmap(mImage , size*ratio, size, true);
}
canvas.drawBitmap(mImage,i * size, 0, null);
}
Currently I have an upper overlay image over a camera surface view.
I would like to do is only output the part of photo that is overlapped with upper layer. So Far I can merge the two image , that means , the overlap layer is on top of the camera view. But how can I get only the overlay part is in output? Thanks
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap resizedBottom;
Bitmap resizedTop;
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap photo=BitmapFactory.decodeByteArray(data, 0, data.length,options);
Matrix matrix = new Matrix();
matrix.postRotate(passDegree);
Bitmap rotatedBitmap = Bitmap.createBitmap(photo, 0, 0,photo.getWidth(), photo.getHeight(), matrix, true);
photo.recycle();
photo = null;
if (h >= w) {
h = w;
} else {
w = h;
}
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
resizedBottom = Bitmap.createScaledBitmap(rotatedBitmap,w,h,false);
resizedTop = Bitmap.createScaledBitmap(topLayer,w,h,false);
} else {
resizedBottom = Bitmap.createScaledBitmap(rotatedBitmap,h,w,false);
resizedTop = Bitmap.createScaledBitmap(topLayer,h,w,false);
}
Canvas c = new Canvas(resizedBottom);
Resources res = getResources();
Drawable drawable1 = new BitmapDrawable(res,resizedBottom);
Drawable drawable2 = new BitmapDrawable(res,resizedTop);
if (res.getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
drawable1.setBounds(0, 0, w, h);
drawable2.setBounds(0, 0, w, h);
} else {
drawable1.setBounds(0, 0, h, w);
drawable2.setBounds(0, 0, h, w);
}
drawable1.draw(c);
drawable2.draw(c);
File sdDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
// Write to SD Card
String fileName = String.format(sdDir+"/%d.jpg", System.currentTimeMillis());
OutputStream os = null;
try {
os = new FileOutputStream(fileName);
resizedBottom.compress(Bitmap.CompressFormat.PNG, 50, os);
} catch(IOException e) {
e.printStackTrace();
}
Log.i("test", "onPictureTaken - jpeg");
resetCam();
}
Spent an afternoon, at the end there is solution , it turn out easier than I thought:
Just
1.Create a temp bitmap to store render result
2.Set PorterDuff.Mode.DST_IN on top layer(In this mode only the overlap area is drawn)
Example code:
Bitmap result = Bitmap.createBitmap(topLayer.getWidth(), topLayer.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mCanvas.drawBitmap(resizedBottom, 0, 0, null);
mCanvas.drawBitmap(topLayer, 0, 0, paint);
paint.setXfermode(null);
Further reading about PorterDuff Mode:
http://www.ibm.com/developerworks/java/library/j-mer0918/
i've really searched through over the entire web before posting. My problem is that i cannot resize bitmap without losing the quality of the image (the quality is really bad and pixelated).
I take the bitmap from camera and then i have to downscale it, so i can upload it to the server much faster.
This is the function that does the sampling
public Bitmap resizeBitmap(Bitmap bitmap){
Canvas canvas = new Canvas();
Bitmap resizedBitmap = null;
if (bitmap !=null) {
int h = bitmap.getHeight();
int w = bitmap.getWidth();
int newWidth=0;
int newHeight=0;
if(h>w){
newWidth = 600;
newHeight = 800;
}
if(w>h){
newWidth = 800;
newHeight = 600;
}
float scaleWidth = ((float) newWidth) / w;
float scaleHeight = ((float) newHeight) / h;
Matrix matrix = new Matrix();
// resize the bit map
matrix.preScale(scaleWidth, scaleHeight);
resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawBitmap(resizedBitmap, matrix, paint);
}
return resizedBitmap;
and this is how i get the image from activity result
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if(resultCode != RESULT_CANCELED){
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
getContentResolver().notifyChange(mImageUri, null);
ContentResolver cr = this.getContentResolver();
try
{
b = android.provider.MediaStore.Images.Media.getBitmap(cr, mImageUri);
Log.d("foto", Integer.toString(b.getWidth()));
Log.d("foto", Integer.toString(b.getHeight()));
addPhoto.setImageBitmap(b);
}
catch (Exception e)
{
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
Log.d("TAG", "Failed to load", e);
}
}
}
I'm starting to think that the best way to get a small picture size is to set the camera resolution. Anyone else can help?
Good downscaling algorithm (not nearest neighbor like) consists of just 2 steps (plus calculation of the exact Rect for input/output images crop):
downscale using BitmapFactory.Options::inSampleSize->BitmapFactory.decodeResource() as close as possible to the resolution that you need but not less than it
get to the exact resolution by downscaling a little bit using Canvas::drawBitmap()
Here is detailed explanation how SonyMobile resolved this task: http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/
Here is the source code of SonyMobile scale utils: http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/
Try below mentioned code for resizing bitmap.
public Bitmap get_Resized_Bitmap(Bitmap bmp, int newHeight, int newWidth) {
int width = bmp.getWidth();
int height = bmp.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 newBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, false);
return newBitmap ;
}
I used this code to downsize my bitmap, and its quality, well.. , was acceptable.
Hope this helps.
Try Bitmap.createScaledBitmap.
It also has an option to filter the source.
the best rescaling method I have come across
the method uses createScaledBitmap whereby the height and width are calculated based on the bitmap height and width and a scale ratio hence quality is not lost
public Bitmap resize(Bitmap imaged, int maxWidth, int maxHeight) {
Bitmap image = imaged;
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > 1) {
finalWidth = Math.round(((float) maxHeight * ratioBitmap));
} else {
finalHeight = Math.round(((float) maxWidth / ratioBitmap));
}
return image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, false);
}
return image;
}
how to use:
Bitmap resizedBitmap = resize(scrBitMap,640,640);