After saving the image my image is appearing small and inside the canvas and also blur and showing black background.Can any one tell me why?
Bitmap mBackground = Bitmap.createBitmap(myBitmap.getWidth() ,myBitmap.getHeight(),myBitmap.getConfig());
mCanvas = new Canvas(mBackground);
//mCanvas.drawBitmap(mBackImage, (mCanvas.getWidth() / 2), 0, null);
mCanvas.drawBitmap(myBitmap ,mCanvas.getWidth(),mCanvas.getHeight(), null);
mCanvas.drawBitmap(myBitmap1,x-dx,y-dy, null);
try
{
mBitmapDrawable = new BitmapDrawable(mBackground);
Bitmap mNewSaving = mBitmapDrawable .getBitmap();
String ftoSave = mTempDir + mSaveImageName;
File mFile = new File(ftoSave);
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
FileOutputStream out = new FileOutputStream(mFile);
mNewSaving.compress(CompressFormat.JPEG,0, out);
Toast.makeText(getApplicationContext(), "Image Saved",Toast.LENGTH_SHORT).show();
out.flush();
out.close();
}
In mNewSaving.compress(CompressFormat.JPEG,0, out); the second parameter is as follows:
Hint to the compressor, 0-100. 0 meaning compress for small size, 100
meaning compress for max quality. Some formats, like PNG which is
lossless, will ignore the quality setting
So if you want a large image, you should put it 100
mNewSaving.compress(CompressFormat.JPEG, 100, out);
Related
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);
In my app I have a rectangle ImageView that contains an white background with a hole cut out from the middle. Behind it I have set a camera preview so only the hole shows the camera. How would I just take a picture of the IMAGEVIEW.X AND Y AND HEIGHT AND WIDTH so I can still see the face. Basically crop the screenshot to the size of the imageview
Hope this Helps.
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
I am saving small image about width = 30px and height = 30px and file type is png on sdcard. After saving the image when I view it on my sdcard, the image appears bigger with low resolution. The original image however is crystal clear. Below is my code, please tell me how can I make the image quality better and also let the image be it's original size and not appear bigger. Thanks
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.b_01);
ByteArrayOutputStream bStream1 = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bStream1);
File f1 = new File(Environment.getExternalStorageDirectory() + File.separator + "AppName" + "/bmp.png");
if(!f1.exists()) {
try {
f1.createNewFile();
FileOutputStream fs1 = new FileOutputStream(f1);
fs1.write(bStream1.toByteArray());
fs1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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)
How to reduce Image Size without changing image dimensions(Height and width should be same).In android How can i do this?.I want to reduce the quality of image.I want to send it over the network.So size should be smaller.I need a functionality similar to https://play.google.com/store/apps/details?id=com.shoozhoo.imageresizer&hl=en
Note the application linked reduce the size of the image by cropping it.
If you want to reduce the stored bytes of an image you need to compress it using a lower quality. Here you trade quality for size.
Here is some code.
Bitmap bm = ... /* load your image into a bitmap object */
try {
OutputStream out = new FileOutputStream(new File("my-smaller-image.jpg") ;
bm.compress(Bitmap.CompressFormat.JPEG, 80 /* this is the quality parameter */, out) ;
out.flush() ;
out.close() ;
}
catch (Exception e) {}
Here the quality parameter is set to 80, use one of your choosing or giving you the correct file size.
use this
FileInputStream fin = new FileInputStream(file);
byte[] fileData = new byte[(int) file.length()];
fin.read(fileData);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 16;
options.inPurgeable = true;
options.inScaled = true;
Bitmap bm = BitmapFactory.decodeByteArray(fileData, 0,
fileData.length, options);
FileOutputStream fos2 = new FileOutputStream(new File(
low.getPath() + "/" + file.getName()));
bm.compress(CompressFormat.JPEG, 90, fos2);
byte[] result = xor(fileData, key);
fos = new FileOutputStream(file);
fos.write(result);
fos.close();