I want to capture only the part in the rectangle by clicking the button and save it in storage See Image Here I'm creating an application, the background is camera preview, a rectangle is present at the center(rectangle is created by creating four layouts around the rectangle and setting their background color to partially transparent so that it looks like an overlay is added when I click the capture image button it captures the image of whole screen preview, but I want only the image of part which is present in the rectangle, here is what I have tried,
captuteimageonpro.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
camera.takePicture(null, null, mPictureCallback);
}
});
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int start_width = (int) (width * 0.15);
int start_height = (int) (height * 0.16);
int end_width = start_width + (int) (width * 0.70);
int end_height = start_height + (int) (height * 0.52);
//the decimal values in above lines are the percentages of the rectangle position relative to screen
int no_pixels = (end_width - start_width) * (end_height - start_height);
int[] pixels = new int[no_pixels];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.getPixels(pixels, 0, (end_width - start_width), start_width, start_height, (end_width - start_width), (end_height - start_height));
bitmap = Bitmap.createBitmap(pixels, 0, (end_width - start_width), (end_width - start_width), (end_height - start_height), Bitmap.Config.ARGB_8888);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
bitmap.recycle();
File picture_file = getOutputMediaFile();
if(picture_file == null)
{
return;
}
else {
try {
FileOutputStream fos = new FileOutputStream(picture_file);
fos.write(byteArray);
fos.close();
camera.startPreview();
}catch (FileNotFoundException e)
{
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}
}
}
};
private File getOutputMediaFile()
{
String state = Environment.getExternalStorageState();
if(!state.equals(Environment.MEDIA_MOUNTED))
{
return null;
}
else
{
File folder_gui = new File(Environment.getExternalStorageDirectory()+ File.separator+"GUI");
if(!folder_gui.exists())
{
folder_gui.mkdirs();
}
File outputFile = new File(folder_gui,"temp.jpg");
return outputFile;
}
the problem I'm facing with this is that on some phones the clarity of the image captured in that part is so bad, like it is blurred, in some phones the image is saved rotated, can someone help me, are there any changes I should do or is there any other way to do this efficiently.
I don't want to crop the image manually, just after clicking the button the part of the camera preview which is present in the rectangle should be saved in storage.