use of net.sourceforge.opencamera.GyroSensor in project OpenCamera by ageback.
the class DrawPreview method onDrawPreview.
public void onDrawPreview(Canvas canvas) {
/*if( MyDebug.LOG )
Log.d(TAG, "onDrawPreview");*/
if (!has_settings) {
if (MyDebug.LOG)
Log.d(TAG, "onDrawPreview: need to update settings");
updateSettings();
}
Preview preview = main_activity.getPreview();
CameraController camera_controller = preview.getCameraController();
int ui_rotation = preview.getUIRotation();
final long time_ms = System.currentTimeMillis();
// see documentation for CameraController.shouldCoverPreview()
if (preview.usingCamera2API() && (camera_controller == null || camera_controller.shouldCoverPreview())) {
p.setColor(Color.BLACK);
canvas.drawRect(0.0f, 0.0f, canvas.getWidth(), canvas.getHeight(), p);
}
if (camera_controller != null && front_screen_flash) {
p.setColor(Color.WHITE);
canvas.drawRect(0.0f, 0.0f, canvas.getWidth(), canvas.getHeight(), p);
}
if (main_activity.getMainUI().inImmersiveMode()) {
if (immersive_mode_everything_pref) {
// in immersive_mode_everything mode)
return;
}
}
if (camera_controller != null && taking_picture && !front_screen_flash && take_photo_border_pref) {
p.setColor(Color.WHITE);
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(stroke_width);
// convert dps to pixels
float this_stroke_width = (5.0f * scale + 0.5f);
p.setStrokeWidth(this_stroke_width);
canvas.drawRect(0.0f, 0.0f, canvas.getWidth(), canvas.getHeight(), p);
// reset
p.setStyle(Paint.Style.FILL);
// reset
p.setStrokeWidth(stroke_width);
}
drawGrids(canvas);
drawCropGuides(canvas);
if (show_last_image && last_thumbnail != null) {
// If changing this code, ensure that pause preview still works when:
// - Taking a photo in portrait or landscape - and check rotating the device while preview paused
// - Taking a photo with lock to portrait/landscape options still shows the thumbnail with aspect ratio preserved
// in case image doesn't cover the canvas (due to different aspect ratios)
p.setColor(Color.rgb(0, 0, 0));
// in case
canvas.drawRect(0.0f, 0.0f, canvas.getWidth(), canvas.getHeight(), p);
last_image_src_rect.left = 0;
last_image_src_rect.top = 0;
last_image_src_rect.right = last_thumbnail.getWidth();
last_image_src_rect.bottom = last_thumbnail.getHeight();
if (ui_rotation == 90 || ui_rotation == 270) {
last_image_src_rect.right = last_thumbnail.getHeight();
last_image_src_rect.bottom = last_thumbnail.getWidth();
}
last_image_dst_rect.left = 0;
last_image_dst_rect.top = 0;
last_image_dst_rect.right = canvas.getWidth();
last_image_dst_rect.bottom = canvas.getHeight();
/*if( MyDebug.LOG ) {
Log.d(TAG, "thumbnail: " + last_thumbnail.getWidth() + " x " + last_thumbnail.getHeight());
Log.d(TAG, "canvas: " + canvas.getWidth() + " x " + canvas.getHeight());
}*/
// use CENTER to preserve aspect ratio
last_image_matrix.setRectToRect(last_image_src_rect, last_image_dst_rect, Matrix.ScaleToFit.CENTER);
if (ui_rotation == 90 || ui_rotation == 270) {
// the rotation maps (0, 0) to (tw/2 - th/2, th/2 - tw/2), so we translate to undo this
float diff = last_thumbnail.getHeight() - last_thumbnail.getWidth();
last_image_matrix.preTranslate(diff / 2.0f, -diff / 2.0f);
}
last_image_matrix.preRotate(ui_rotation, last_thumbnail.getWidth() / 2.0f, last_thumbnail.getHeight() / 2.0f);
canvas.drawBitmap(last_thumbnail, last_image_matrix, p);
}
doThumbnailAnimation(canvas, time_ms);
drawUI(canvas, time_ms);
drawAngleLines(canvas);
doFocusAnimation(canvas, time_ms);
CameraController.Face[] faces_detected = preview.getFacesDetected();
if (faces_detected != null) {
// Yellow 500
p.setColor(Color.rgb(255, 235, 59));
p.setStyle(Paint.Style.STROKE);
p.setStrokeWidth(stroke_width);
for (CameraController.Face face : faces_detected) {
// Android doc recommends filtering out faces with score less than 50 (same for both Camera and Camera2 APIs)
if (face.score >= 50) {
canvas.drawRect(face.rect, p);
}
}
// reset
p.setStyle(Paint.Style.FILL);
}
if (enable_gyro_target_spot) {
GyroSensor gyroSensor = main_activity.getApplicationInterface().getGyroSensor();
if (gyroSensor.isRecording()) {
gyroSensor.getRelativeInverseVector(transformed_gyro_direction, gyro_direction);
// note that although X of gyro_direction represents left to right on the device, because we're in landscape mode,
// this is y coordinates on the screen
float angle_x = -(float) Math.asin(transformed_gyro_direction[1]);
float angle_y = -(float) Math.asin(transformed_gyro_direction[0]);
if (Math.abs(angle_x) < 0.5f * Math.PI && Math.abs(angle_y) < 0.5f * Math.PI) {
float camera_angle_x = preview.getViewAngleX();
float camera_angle_y = preview.getViewAngleY();
float angle_scale_x = (float) (canvas.getWidth() / (2.0 * Math.tan(Math.toRadians((camera_angle_x / 2.0)))));
float angle_scale_y = (float) (canvas.getHeight() / (2.0 * Math.tan(Math.toRadians((camera_angle_y / 2.0)))));
angle_scale_x *= preview.getZoomRatio();
angle_scale_y *= preview.getZoomRatio();
// angle_scale is already in pixels rather than dps
float distance_x = angle_scale_x * (float) Math.tan(angle_x);
// angle_scale is already in pixels rather than dps
float distance_y = angle_scale_y * (float) Math.tan(angle_y);
p.setColor(Color.WHITE);
// draw spot for the centre of the screen, to help the user orient the device
drawGyroSpot(canvas, 0.0f, 0.0f);
p.setColor(Color.BLUE);
drawGyroSpot(canvas, distance_x, distance_y);
}
}
}
}
Aggregations