Last active
August 9, 2022 03:56
-
-
Save Shaked/f66134ea62755c0cbd69 to your computer and use it in GitHub Desktop.
Android Camera
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class CameraInfo { | |
/** | |
+ The facing of the camera is opposite to that of the screen. | |
*/ | |
public static final int CAMERA_FACING_BACK = 0; | |
/** | |
+ The facing of the camera is the same as that of the screen. | |
*/ | |
public static final int CAMERA_FACING_FRONT = 1; | |
... | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (null != camera) { | |
try { | |
camera.stopPreview(); | |
camera.setPreviewCallback(null); | |
camera.release(); | |
camera = null; | |
} catch(RuntimeException e){ | |
//handle exception | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CroppingTool { | |
private static final String TAG = "CroppingTool"; | |
public static byte[] cropRect( | |
byte data[], | |
int previewTop, | |
int previewLeft, | |
int previewBottom, | |
int previewRight, | |
int gridRight, | |
int gridBottom, | |
int gridTop | |
) { | |
Bitmap bitmapPicture = BitmapFactory.decodeByteArray(data, 0, data.length); | |
// Here previewRect is a rectangle which holds the camera's preview size, | |
// pictureRect and nativeResRect hold the camera's picture size and its | |
// native resolution, respectively. | |
RectF previewRect = new RectF(previewLeft, previewTop, previewRight, previewBottom); | |
RectF pictureRect = new RectF(0, 0, bitmapPicture.getWidth(), bitmapPicture.getHeight()); | |
RectF resultRect = new RectF(0, gridTop, gridRight, gridBottom); | |
final Matrix scaleMatrix = new Matrix(); | |
// map the result rectangle to the new coordinates | |
scaleMatrix.mapRect(resultRect); | |
// create a matrix which scales coordinates of picture size rectangle into the | |
// camera's native resolution. | |
scaleMatrix.setRectToRect(previewRect, pictureRect, Matrix.ScaleToFit.CENTER); | |
// invert it, so that we get the matrix which downscales the rectangle from | |
// the native resolution to the actual picture size | |
scaleMatrix.mapRect(resultRect); | |
Bitmap cropped = Bitmap.createBitmap( | |
bitmapPicture, | |
(int) resultRect.left, | |
(int) resultRect.top, | |
(int) (resultRect.width()), | |
(int) (resultRect.height()) | |
); | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
// create a file to write bitmap data | |
cropped.compress(Bitmap.CompressFormat.JPEG, 100, bos); | |
// Convert bitmap to byte array | |
byte[] bm = bos.toByteArray(); | |
return bm; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Device { | |
private static int orientation; | |
public static void load() { | |
//check if emulator is running | |
if (Build.BRAND.toLowerCase().contains("generic")) { | |
Device.orientation = 0; | |
} else { | |
Device.orientation = 90; | |
} | |
} | |
public static int getOrientation() { | |
return Device.orientation; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) { | |
final double ASPECT_TOLERANCE = 0.1; | |
double targetRatio = (double) h / w; | |
if (sizes == null) { | |
return null; | |
} | |
Camera.Size optimalSize = null; | |
double minDiff = Double.MAX_VALUE; | |
int targetHeight = h; | |
for (Camera.Size size : sizes) { | |
double ratio = (double) size.height / size.width; | |
if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) { | |
continue; | |
} | |
if (Math.abs(size.height - targetHeight) < minDiff) { | |
optimalSize = size; | |
minDiff = Math.abs(size.height - targetHeight); | |
} | |
} | |
if (optimalSize == null) { | |
minDiff = Double.MAX_VALUE; | |
for (Camera.Size size : sizes) { | |
if (Math.abs(size.height - targetHeight) < minDiff) { | |
optimalSize = size; | |
minDiff = Math.abs(size.height - targetHeight); | |
} | |
} | |
} | |
return optimalSize; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static byte[] rotate(byte[] data) { | |
Matrix matrix = new Matrix(); | |
//Device.getOrientation() is used in order to support the emulator and an actual device | |
matrix.postRotate(Device.getOrientation()); | |
Bitmap bitmap = BitmapFactory.decodeByteArray(data, INT_START, data.length); | |
if (bitmap.getWidth() < bitmap.getHeight()) { | |
//no rotation needed | |
return data; | |
} | |
Bitmap rotatedBitmap = Bitmap.createBitmap( | |
bitmap, 0, 0, bitmap.getWidth(), | |
bitmap.getHeight(), matrix, true | |
); | |
ByteArrayOutputStream blob = new ByteArrayOutputStream(); | |
rotatedBitmap.compress(CompressFormat.JPEG, 100, blob); | |
byte[] bm = blob.toByteArray(); | |
return bm; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void setCameraLayout(int width, int height) { | |
float newProportion = (float) width / (float) height; | |
// Get the width of the screen | |
Point point = new Point(); | |
this.activity.getWindowManager().getDefaultDisplay().getSize(point); | |
int screenWidth = point.x; | |
int screenHeight = point.y; | |
float screenProportion = (float) screenWidth / (float) screenHeight; | |
// Get the SurfaceView layout parameters | |
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) this | |
.getLayoutParams(); | |
float scaleFactor = 1; | |
/* | |
* assume width is smaller than height in screen and in input | |
* parameters. Therefore if newProportion > screenProportion then | |
* The desire proportion is more wider than higher therefore we match it against | |
* screen width and scale it height with the new proportion | |
* | |
*/ | |
if (newProportion > screenProportion) { | |
lp.width = screenWidth; | |
lp.height = (int) ((float) screenWidth * (1 / newProportion)); | |
scaleFactor = (screenHeight / lp.height); // calculate the factor to make it full screen | |
} else { | |
lp.width = (int) (newProportion * (float) screenHeight); | |
lp.height = screenHeight; | |
scaleFactor = screenWidth / lp.width; // calculate the factor to make it full screen. | |
} | |
lp.width = (int) (lp.width * scaleFactor); | |
lp.height = (int) (lp.height * scaleFactor); | |
adjustFooterToFullScreen(screenHeight, lp); | |
lp.gravity = Gravity.CENTER; | |
sv.setLayoutParams(lp); | |
this.footer.setBottom(sv.getBottom()); | |
this.footer.setLayoutParams( | |
new LinearLayout.LayoutParams( | |
screenWidth, | |
this.footer.getHeight() | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment