We will be taking a picture with this library and
create a Bitmap and set it in an ImageView as follow
public
void onPictureTaken(CameraView cameraView, byte[] data) {
Bitmap bitmap =
BitmapFactory.decodeByteArray(data, 0, data.length);
imageView.setImageBitmap(bitmap);
}
This will work fine on most of the devices however
there are some exceptional devices too, where the picture orientation
appears to be incorrectly rotated 90 degrees to the left when the device
orientations is portrait.
Solution to overcome this issue in your application
Trick : Check if the width > height of the image then rotate by 90
Follow the steps:
private
static int fixOrientation(Bitmap bitmap) {
if (bitmap.getWidth() >
bitmap.getHeight()) {
return 90;
}
return 0;
}
Call this method to apply the rotation if needed
public
static Bitmap flipIMage(Bitmap bitmap) {
//Moustafa: fix issue of image
reflection due to front camera settings
Matrix matrix = new Matrix();
int rotation = fixOrientation(bitmap);
matrix.postRotate(rotation);
matrix.preScale(-1, 1);
return Bitmap.createBitmap(bitmap, 0,
0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
NOTE : Scale by (-1,1) is because I
use front camera and need to fix the reflection issue of camera.
Implement in your coding:
public
void onPictureTaken(CameraView cameraView, byte[] data) {
Bitmap bitmap =
BitmapFactory.decodeByteArray(data, 0, data.length);
int oreo = fixOrientation(bitmap);
if(oreo==90){
bitmap = flipIMage(thumbnail, oreo);
imageView.setImageBitmap(bitmap);
}else{
bitmap = flipIMage(thumbnail, oreo);
imageView.setImageBitmap(bitmap);
}
imageView.setImageBitmap(bitmap);
}
private
static int fixOrientation(Bitmap bitmap) {
if (bitmap.getWidth() >
bitmap.getHeight()) {
return 90;
}
return 0;
}
public
static Bitmap flipIMage(Bitmap bitmap, int moreo) {
//Moustafa: fix issue of image reflection
due to front camera settings
Matrix matrix = new Matrix();
Bitmap bitReturn = null;
if(moreo==90){
int rotation = fixOrientation(bitmap);
matrix.postRotate(rotation);
matrix.preScale(-1, 1);
bitReturn = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}else{
matrix.preScale(-1, 1);
bitReturn = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}
return bitReturn;
}
Approach 2:
Approach 2:
if(file!=null){ imageStoragePath = file.getAbsolutePath(); thumbnail = optimizeBitmap(BITMAP_SAMPLE_SIZE, imageStoragePath);
try { ExifInterface ei = new ExifInterface(imageStoragePath); int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); Bitmap rotatedBitmap = null; switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90: rotatedBitmap = rotateImage(thumbnail, 90); break;
case ExifInterface.ORIENTATION_ROTATE_180: rotatedBitmap = rotateImage(thumbnail, 180); break;
case ExifInterface.ORIENTATION_ROTATE_270: rotatedBitmap = rotateImage(thumbnail, 270); break;
case ExifInterface.ORIENTATION_NORMAL: default: rotatedBitmap = thumbnail; }
mCameraPreview.setImageBitmap(rotatedBitmap);
} catch (IOException e) { e.printStackTrace(); }
Bitmap screenshot = getScreenShot(screenshotlay); store(screenshot,"lifecam.png");}
__________________________________________________________________________
Happy Coding...
Comments
Post a Comment