Android scaling copy of image instead of changing source image - android

I have the following code for getting small sized avatar image:
Bitmap b= BitmapFactory.decodeFile(selectedImagePath);
File file = new File(selectedImagePath);
Bitmap out = Bitmap.createScaledBitmap(b, 128, 128, false);
FileOutputStream fOut;
try {
fOut = new FileOutputStream(file);
out.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
b.recycle();
out.recycle();
}
The problem is that after executing this code, my image in gallery is rescaled too, but I must leave it without any changes.
I tried to use copy of Bitmap image like this:
Bitmap bmp2 = b.copy(b.getConfig(), true);
Bitmap out = Bitmap.createScaledBitmap(bpm2, 128, 128, false);
or
Bitmap bmp2 = Bitmap.createBitmap(b);
Bitmap out = Bitmap.createScaledBitmap(bpm2, 128, 128, false
But my original image still changes. How could I obtain new, independent copy of this image?

You are loading the image from "selectedImagePath" and then your output file points to the same path, when you call compress() you overwrite your original image.
Try making a new name for the resized image.
fOut = new FileOutputStream(renamedFile);

Related

Android Creating an image from an API response [duplicate]

I need to create .jpeg/.png file on my Android application programmatically. I have simple image (black background), and it need to write some text on it programmatically. How can I do it? Is it possible?
It's definately possible.
To write text on an image you have to load the image in to a Bitmap object. Then draw on that bitmap with the Canvas and Paint functions. When you're done drawing you simply output the Bitmap to a file.
If you're just using a black background, it's probably better for you to simply create a blank bitmap on a canvas, fill it black, draw text and then dump to a Bitmap.
I used this tutorial to learn the basics of the canvas and paint.
This is the code that you'll be looking for to turn the canvas in to an image file:
OutputStream os = null;
try {
File file = new File(dir, "image" + System.currentTimeMillis() + ".png");
os = new FileOutputStream(file);
finalBMP.compress(CompressFormat.PNG, 100, os);
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
screenGrabFilePath = file.getPath();
} catch(IOException e) {
finalBMP.recycle(); // this is very important. make sure you always recycle your bitmap when you're done with it.
Log.e("combineImages", "problem combining images", e);
}
Yes, see here
Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
You can also use awt's Graphics2D with this compatibility project
Using Graphics2d you can create a PNG image as well:
public class Imagetest {
public static void main(String[] args) throws IOException {
File path = new File("image/base/path");
BufferedImage img = new BufferedImage(100, 100,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.setColor(Color.YELLOW);
g2d.drawLine(0, 0, 50, 50);
g2d.setColor(Color.BLACK);
g2d.drawLine(50, 50, 0, 100);
g2d.setColor(Color.RED);
g2d.drawLine(50, 50, 100, 0);
g2d.setColor(Color.GREEN);
g2d.drawLine(50, 50, 100, 100);
ImageIO.write(img, "PNG", new File(path, "1.png"));
}
}

iText, FileNotFoundException when adding image to pdf

I am trying to add an image to my pdf using iText and when following the iTexts documentation on this its just simply telling me to do this:
Image img = Image.getInstance("res/drawable/toplogos.png");
document.add(img);
But I am getting FileNotFoundException. Why is that? The file is in the drawable folder.
Thanks.
try this, hope this will help you
Drawable d = getResources().getDrawable(R.drawable.toplogos)
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bmp = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
document.add(image);

Converting images in Android

I am facing a few problems here. I have a socket server and my app uploads images to it. Now if I say I have 1 million customers each one 1mb for image, I should leave 1 terabyte for just profile images, which is too much. I am seeking a way to convert all image files to max 100 kb size, is such thing possible? If so what should I search for?
And also I'm selecting image files from disk, like this:
File file = new File("storage/sdcard/LifeMatePrivate/ProfileImage
/ProfileImage,imagechange_1,"+imagenamer+".jpg")
But as you see it just selects images with prefix.jpg. Is there a way I can the file with any extension? Thanks.
public void SaveImage(View v){
System.out.println(path);
String Path = path;
//File file=new File("storage/sdcard/Pictures/reza2.jpg");
Bitmap bitmap = BitmapFactory.decodeFile("storage/sdcard/Pictures
/reza2.jpg");
// you can change the format of you image compressed for what do you
want;
//now it is set up to 640 x 960;
Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 960, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// CompressFormat set up to JPG, you can change to PNG or whatever you
want;
bmpCompressed.compress(CompressFormat.PNG, 100, bos);
// Intent returnIntent = new Intent();
// returnIntent.putExtra("result",Path);
// setResult(RESULT_OK,returnIntent);
// Log.i("Image",path);
finish();
}
this code is inside an activity stared with StartActivityForResult
Try this to compress your images..
Bitmap bitmap = BitmapFactory.decodeFile(imagefilepath);
// you can change the format of you image compressed for what do you want;
//now it is set up to 640 x 960;
Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 960, true);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// CompressFormat set up to JPG, you can change to PNG or whatever you want;
bmpCompressed.compress(CompressFormat.JPEG, 100, bos);
now bmpCompressed is a compressed file format
There is 2 ways to scale a bitmap :
With given size :
Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);
Sampling image using :
BitmapFactory.decodeFile(file, options)

Android: How can I save image files that are not (JPEG & PNG)? [after rotate]

I am trying to rotate an image from sdcard and then save back to sdcard.
I can do that for ".jpg" format by using ExifInterface class:
exif = new ExifInterface(filepath);
exif.setAttribute(ExifInterface.TAG_ORIENTATION, Integer.toString(orientation));
exif.saveAttributes();
For ".png" files, I would have to actually rotate and save:
Bitmap bitmap = BitmapFactory.decodeFile(filepath);
Matrix matrix = new Matrix();
matrix.postRotate(degrees);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
FileOutputStream stream = new FileOutputStream(fileLocation);
bitmap.compress(CompressFormat.PNG, 100, stream);
What about ".bmp", ".tiff", ".gif" ??
It seems like CompressFormat only supports 'CompressFormat.PNG' and 'CompressFormat.JPG'.
Is this limitation?
Yes Limited To JPG , PNG , WEBP
http://developer.android.com/reference/android/graphics/Bitmap.CompressFormat.html
Hey just give the name to .bmp
Do this:
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.PNG, 40, bytes);
//you can create a new file name "test.BMP" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "**test.bmp**")
it'll sound that IM JUST FOOLING AROUND but try it once it'll get saved in bmp foramt..Cheers

Retrieve "Clean" Image Out Of ImageView

I've Image stored in ImageView and after several processes (flip, rotate, fix color, etc) it's saved back as new file. However, I just realize that when I just load into ImageView and soon after that directly save the result, I got different result. Take a look at attached image for reference.
image source
image result
Here's how I extract image from ImageView:
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + bufferPath;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
selectedImage.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(selectedImage.getDrawingCache());
selectedImage.setDrawingCacheEnabled(false);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
imageFile = new File( filePath );
//write the bytes in file
FileOutputStream fo = new FileOutputStream(imageFile);
fo.write(bytes.toByteArray());
Is there any workaround to make resulting image the same as source? different component, maybe?
Hold a backing Bitmap (originally the source) that you carry out your manipulations on.
Use the ImageView just to show this Bitmap - do not manipulate the post-scaled version that is held in the ImageView's drawing cache.

Categories

Resources