Screenshoting certain part of screen programmatically - android

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();
}
}

Related

How to get the bitmap of current layout(For sharing the screen as image) instead of taking screenshot?

How to get the bitmap of current layout(For sharing the screen as image) instead of taking screenshot? I'm getting the view of layout using following line,
I can able to get bitmap of this layout but the image is looking as empty(I got blank image). This is my code.
View cardShareView = getLayoutInflater().inflate(R.layout.activity_cad_profile, null);
boolean imageResult=saveImageInLocal(getBitmapFromView(cardShareView), "cad_share.png") == true
public static Bitmap getBitmapFromView(View view) {
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(canvas);
return bitmap;
}
private boolean saveImageInLocal(Bitmap imageData, String filename) {
String iconsStoragePath = Environment.getExternalStorageDirectory() + "/KKMC/";
File sdIconStorageDir = new File(iconsStoragePath);
//create storage directories, if they don't exist
sdIconStorageDir.mkdirs();
try {
filePath = sdIconStorageDir.toString() + filename;
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);
//choose another format if PNG doesn't suit you
imageData.compress(Bitmap.CompressFormat.PNG, 70, bos);
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
} catch (IOException e) {
Log.w("TAG", "Error saving image file: " + e.getMessage());
return false;
}
return true;
}
Advance Thanks for your response.
Expecting this image
Bit I got this image
Finally, I created duplicate layout(activity_cad_profile.xml) for take the full view as image which is loaded at the time of main layout is loading. I don't know this is correct way or not. But it's working fine for me.

Image saved on sdcard appears bigger

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();
}
}

Save layout as an image

I have a LinearLayout and I am want to save the contents of that view as an image. I have it half working.
File imageFile;
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/a.png";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
Bitmap bitmap;
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 10, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
With the above code it will save a copy of the view, but only what's seen on the device screen. I have more information on the view that I want saved. The code above from here and this will only save the information seen on the screen as an image. I want all the information saved, even the information that is not on the screen (where you need to scroll to see).
How can I achieve that?
Another option is to use:
Bitmap b = Bitmap.createBitmap(width, height....)
v1.setLayoutParams // Full width and height of content
Canvas c = new Canvas(b);
v1.draw(c); // You now have full bitmap
saveBitmap(b);
Run a measure/layout pass on it and draw it to a canvas. Suppose your parent was called "view" and was a vertical LinearLayout:
view.measure(someWidth, MeasureSpec.UNSPECIFIED);
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.draw(c);

android load small image from sd card to fill in imageview(full height width)

I want to load a bitmap from sdcard with fill full height width of imageview i have done this code
im = (ImageView) findViewById(R.id.imageView1);
im.setDrawingCacheEnabled(true);
b = im.getDrawingCache();
// im.setImageBitmap(b);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "TEST1.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
File imgFile = new File(Environment
.getExternalStorageDirectory()
+ File.separator
+ "TEST1.jpg");
if (imgFile.exists()) {
im.setScaleType(ScaleType.CENTER_CROP);
// im.setScaleType(ScaleType.FIT_XY);//this is also not giving me proper o/p
// im.setVisibility(View.GONE);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile
.getAbsolutePath());
im.setImageBitmap(myBitmap);
}
but it not working
It display like this But Actually it must display in full screen
Actually what i want to do is load any bitmap resizing it and want to display only selected portion of image in same imageview but selected image portion in fill imageview
Updated: Just saw the comment saying you want to zoom etc.
Save yourself the headache of reinventing the wheel and check-out Chris Banes' great PhotoView library.

Image is seen smaller after saved in sdcard

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);

Categories

Resources