Search in sources :

Example 11 with CameraController

use of net.sourceforge.opencamera.CameraController.CameraController in project OpenCamera by ageback.

the class DrawPreview method drawCropGuides.

private void drawCropGuides(Canvas canvas) {
    Preview preview = main_activity.getPreview();
    CameraController camera_controller = preview.getCameraController();
    if (preview.isVideo() || preview_size_wysiwyg_pref) {
        String preference_crop_guide = sharedPreferences.getString(PreferenceKeys.ShowCropGuidePreferenceKey, "crop_guide_none");
        if (camera_controller != null && preview.getTargetRatio() > 0.0 && !preference_crop_guide.equals("crop_guide_none")) {
            p.setStyle(Paint.Style.STROKE);
            p.setStrokeWidth(stroke_width);
            // Yellow 500
            p.setColor(Color.rgb(255, 235, 59));
            double crop_ratio = -1.0;
            switch(preference_crop_guide) {
                case "crop_guide_1":
                    crop_ratio = 1.0;
                    break;
                case "crop_guide_1.25":
                    crop_ratio = 1.25;
                    break;
                case "crop_guide_1.33":
                    crop_ratio = 1.33333333;
                    break;
                case "crop_guide_1.4":
                    crop_ratio = 1.4;
                    break;
                case "crop_guide_1.5":
                    crop_ratio = 1.5;
                    break;
                case "crop_guide_1.78":
                    crop_ratio = 1.77777778;
                    break;
                case "crop_guide_1.85":
                    crop_ratio = 1.85;
                    break;
                case "crop_guide_2.33":
                    crop_ratio = 2.33333333;
                    break;
                case "crop_guide_2.35":
                    // actually 1920:817
                    crop_ratio = 2.35006120;
                    break;
                case "crop_guide_2.4":
                    crop_ratio = 2.4;
                    break;
            }
            if (crop_ratio > 0.0 && Math.abs(preview.getTargetRatio() - crop_ratio) > 1.0e-5) {
                /*if( MyDebug.LOG ) {
		    			Log.d(TAG, "crop_ratio: " + crop_ratio);
		    			Log.d(TAG, "preview_targetRatio: " + preview_targetRatio);
		    			Log.d(TAG, "canvas width: " + canvas.getWidth());
		    			Log.d(TAG, "canvas height: " + canvas.getHeight());
		    		}*/
                int left = 1, top = 1, right = canvas.getWidth() - 1, bottom = canvas.getHeight() - 1;
                if (crop_ratio > preview.getTargetRatio()) {
                    // crop ratio is wider, so we have to crop top/bottom
                    double new_hheight = ((double) canvas.getWidth()) / (2.0f * crop_ratio);
                    top = (canvas.getHeight() / 2 - (int) new_hheight);
                    bottom = (canvas.getHeight() / 2 + (int) new_hheight);
                } else {
                    // crop ratio is taller, so we have to crop left/right
                    double new_hwidth = (((double) canvas.getHeight()) * crop_ratio) / 2.0f;
                    left = (canvas.getWidth() / 2 - (int) new_hwidth);
                    right = (canvas.getWidth() / 2 + (int) new_hwidth);
                }
                canvas.drawRect(left, top, right, bottom, p);
            }
            // reset
            p.setStyle(Paint.Style.FILL);
        }
    }
}
Also used : CameraController(net.sourceforge.opencamera.CameraController.CameraController) Preview(net.sourceforge.opencamera.Preview.Preview) Paint(android.graphics.Paint)

Example 12 with CameraController

use of net.sourceforge.opencamera.CameraController.CameraController in project OpenCamera by ageback.

the class DrawPreview method drawUI.

/**
 * This includes drawing of the UI that requires the canvas to be rotated according to the preview's
 *  current UI rotation.
 */
private void drawUI(Canvas canvas, long time_ms) {
    Preview preview = main_activity.getPreview();
    CameraController camera_controller = preview.getCameraController();
    int ui_rotation = preview.getUIRotation();
    boolean ui_placement_right = main_activity.getMainUI().getUIPlacementRight();
    boolean has_level_angle = preview.hasLevelAngle();
    double level_angle = preview.getLevelAngle();
    boolean has_geo_direction = preview.hasGeoDirection();
    double geo_direction = preview.getGeoDirection();
    canvas.save();
    canvas.rotate(ui_rotation, canvas.getWidth() / 2.0f, canvas.getHeight() / 2.0f);
    if (camera_controller != null && !preview.isPreviewPaused()) {
        /*canvas.drawText("PREVIEW", canvas.getWidth() / 2,
					canvas.getHeight() / 2, p);*/
        // convert dps to pixels
        int text_y = (int) (20 * scale + 0.5f);
        // fine tuning to adjust placement of text with respect to the GUI, depending on orientation
        int text_base_y = 0;
        if (ui_rotation == (ui_placement_right ? 0 : 180)) {
            text_base_y = canvas.getHeight() - (int) (0.5 * text_y);
        } else if (ui_rotation == (ui_placement_right ? 180 : 0)) {
            // leave room for GUI icons
            text_base_y = canvas.getHeight() - (int) (2.5 * text_y);
        } else if (ui_rotation == 90 || ui_rotation == 270) {
            // text_base_y = canvas.getHeight() + (int)(0.5*text_y);
            /*ImageButton view = (ImageButton)main_activity.findViewById(R.id.take_photo);
				// align with "top" of the take_photo button, but remember to take the rotation into account!
				view.getLocationOnScreen(gui_location);
				int view_left = gui_location[0];
				preview.getView().getLocationOnScreen(gui_location);
				int this_left = gui_location[0];
				// diff_x is the difference from the centre of the canvas to the position we want
				int diff_x = view_left - ( this_left + canvas.getWidth()/2 );
				*/
            /*if( MyDebug.LOG ) {
					Log.d(TAG, "view left: " + view_left);
					Log.d(TAG, "this left: " + this_left);
					Log.d(TAG, "canvas is " + canvas.getWidth() + " x " + canvas.getHeight());
				}*/
            // diff_x is the difference from the centre of the canvas to the position we want
            // assumes canvas is centered
            // avoids calling getLocationOnScreen for performance
            // convert dps to pixels
            int diff_x = preview.getView().getRootView().getRight() / 2 - (int) (100 * scale + 0.5f);
            int max_x = canvas.getWidth();
            if (ui_rotation == 90) {
                // so we don't interfere with the top bar info (datetime, free memory, ISO)
                max_x -= (int) (2.5 * text_y);
            }
            /*if( MyDebug.LOG ) {
					Log.d(TAG, "root view right: " + preview.getView().getRootView().getRight());
					Log.d(TAG, "diff_x: " + diff_x);
					Log.d(TAG, "canvas.getWidth()/2 + diff_x: " + (canvas.getWidth()/2+diff_x));
					Log.d(TAG, "max_x: " + max_x);
				}*/
            if (canvas.getWidth() / 2 + diff_x > max_x) {
                // in case goes off the size of the canvas, for "black bar" cases (when preview aspect ratio != screen aspect ratio)
                diff_x = max_x - canvas.getWidth() / 2;
            }
            text_base_y = canvas.getHeight() / 2 + diff_x - (int) (0.5 * text_y);
        }
        boolean draw_angle = has_level_angle && show_angle_pref;
        boolean draw_geo_direction = has_geo_direction && show_geo_direction_pref;
        if (draw_angle) {
            int color = Color.WHITE;
            // convert dps to pixels
            p.setTextSize(14 * scale + 0.5f);
            int pixels_offset_x;
            if (draw_geo_direction) {
                // convert dps to pixels
                pixels_offset_x = -(int) (35 * scale + 0.5f);
                p.setTextAlign(Paint.Align.LEFT);
            } else {
                // p.setTextAlign(Paint.Align.CENTER);
                // slightly better for performance to use Align.LEFT, due to avoid measureText() call in drawTextWithBackground()
                // convert dps to pixels
                pixels_offset_x = -(int) ((level_angle < 0 ? 16 : 14) * scale + 0.5f);
                p.setTextAlign(Paint.Align.LEFT);
            }
            if (Math.abs(level_angle) <= close_level_angle) {
                color = angle_highlight_color_pref;
                p.setUnderlineText(true);
            }
            if (angle_string == null || time_ms > this.last_angle_string_time + 500) {
                // update cached string
                /*if( MyDebug.LOG )
						Log.d(TAG, "update angle_string: " + angle_string);*/
                last_angle_string_time = time_ms;
                String number_string = formatLevelAngle(level_angle);
                // String number_string = "" + level_angle;
                angle_string = number_string + (char) 0x00B0;
                cached_angle = level_angle;
            // String angle_string = "" + level_angle;
            }
            // applicationInterface.drawTextWithBackground(canvas, p, angle_string, color, Color.BLACK, canvas.getWidth() / 2 + pixels_offset_x, text_base_y, MyApplicationInterface.Alignment.ALIGNMENT_BOTTOM, ybounds_text, true);
            if (text_bounds_angle_single == null) {
                if (MyDebug.LOG)
                    Log.d(TAG, "compute text_bounds_angle_single");
                text_bounds_angle_single = new Rect();
                String bounds_angle_string = "-9.0" + (char) 0x00B0;
                p.getTextBounds(bounds_angle_string, 0, bounds_angle_string.length(), text_bounds_angle_single);
            }
            if (text_bounds_angle_double == null) {
                if (MyDebug.LOG)
                    Log.d(TAG, "compute text_bounds_angle_double");
                text_bounds_angle_double = new Rect();
                String bounds_angle_string = "-45.0" + (char) 0x00B0;
                p.getTextBounds(bounds_angle_string, 0, bounds_angle_string.length(), text_bounds_angle_double);
            }
            applicationInterface.drawTextWithBackground(canvas, p, angle_string, color, Color.BLACK, canvas.getWidth() / 2 + pixels_offset_x, text_base_y, MyApplicationInterface.Alignment.ALIGNMENT_BOTTOM, null, true, Math.abs(cached_angle) < 10.0 ? text_bounds_angle_single : text_bounds_angle_double);
            p.setUnderlineText(false);
        }
        if (draw_geo_direction) {
            int color = Color.WHITE;
            // convert dps to pixels
            p.setTextSize(14 * scale + 0.5f);
            int pixels_offset_x;
            if (draw_angle) {
                // convert dps to pixels
                pixels_offset_x = (int) (10 * scale + 0.5f);
                p.setTextAlign(Paint.Align.LEFT);
            } else {
                // p.setTextAlign(Paint.Align.CENTER);
                // slightly better for performance to use Align.LEFT, due to avoid measureText() call in drawTextWithBackground()
                // convert dps to pixels
                pixels_offset_x = -(int) (14 * scale + 0.5f);
                p.setTextAlign(Paint.Align.LEFT);
            }
            float geo_angle = (float) Math.toDegrees(geo_direction);
            if (geo_angle < 0.0f) {
                geo_angle += 360.0f;
            }
            String string = "" + Math.round(geo_angle) + (char) 0x00B0;
            applicationInterface.drawTextWithBackground(canvas, p, string, color, Color.BLACK, canvas.getWidth() / 2 + pixels_offset_x, text_base_y, MyApplicationInterface.Alignment.ALIGNMENT_BOTTOM, ybounds_text, true);
        }
        if (preview.isOnTimer()) {
            long remaining_time = (preview.getTimerEndTime() - time_ms + 999) / 1000;
            if (MyDebug.LOG)
                Log.d(TAG, "remaining_time: " + remaining_time);
            if (remaining_time > 0) {
                // convert dps to pixels
                p.setTextSize(42 * scale + 0.5f);
                p.setTextAlign(Paint.Align.CENTER);
                String time_s;
                if (remaining_time < 60) {
                    // simpler to just show seconds when less than a minute
                    time_s = "" + remaining_time;
                } else {
                    time_s = getTimeStringFromSeconds(remaining_time);
                }
                // Red 500
                applicationInterface.drawTextWithBackground(canvas, p, time_s, Color.rgb(244, 67, 54), Color.BLACK, canvas.getWidth() / 2, canvas.getHeight() / 2);
            }
        } else if (preview.isVideoRecording()) {
            long video_time = preview.getVideoTime();
            String time_s = getTimeStringFromSeconds(video_time / 1000);
            /*if( MyDebug.LOG )
					Log.d(TAG, "video_time: " + video_time + " " + time_s);*/
            // convert dps to pixels
            p.setTextSize(14 * scale + 0.5f);
            p.setTextAlign(Paint.Align.CENTER);
            // avoid overwriting the zoom, and also allow a bit extra space
            int pixels_offset_y = 3 * text_y;
            // Red 500
            int color = Color.rgb(244, 67, 54);
            if (main_activity.isScreenLocked()) {
                // writing in reverse order, bottom to top
                applicationInterface.drawTextWithBackground(canvas, p, getContext().getResources().getString(R.string.screen_lock_message_2), color, Color.BLACK, canvas.getWidth() / 2, text_base_y - pixels_offset_y);
                pixels_offset_y += text_y;
                applicationInterface.drawTextWithBackground(canvas, p, getContext().getResources().getString(R.string.screen_lock_message_1), color, Color.BLACK, canvas.getWidth() / 2, text_base_y - pixels_offset_y);
                pixels_offset_y += text_y;
            }
            if (!preview.isVideoRecordingPaused() || ((int) (time_ms / 500)) % 2 == 0) {
                // if video is paused, then flash the video time
                applicationInterface.drawTextWithBackground(canvas, p, time_s, color, Color.BLACK, canvas.getWidth() / 2, text_base_y - pixels_offset_y);
            }
        } else if (taking_picture && capture_started) {
            if (camera_controller.isManualISO()) {
                // only show "capturing" text with time for manual exposure time >= 0.5s
                long exposure_time = camera_controller.getExposureTime();
                if (exposure_time >= 500000000L) {
                    if (((int) (time_ms / 500)) % 2 == 0) {
                        // convert dps to pixels
                        p.setTextSize(14 * scale + 0.5f);
                        p.setTextAlign(Paint.Align.CENTER);
                        // avoid overwriting the zoom, and also allow a bit extra space
                        int pixels_offset_y = 3 * text_y;
                        // Red 500
                        int color = Color.rgb(244, 67, 54);
                        applicationInterface.drawTextWithBackground(canvas, p, getContext().getResources().getString(R.string.capturing), color, Color.BLACK, canvas.getWidth() / 2, text_base_y - pixels_offset_y);
                    }
                }
            }
        } else if (image_queue_full) {
            if (((int) (time_ms / 500)) % 2 == 0) {
                // convert dps to pixels
                p.setTextSize(14 * scale + 0.5f);
                p.setTextAlign(Paint.Align.CENTER);
                // avoid overwriting the zoom, and also allow a bit extra space
                int pixels_offset_y = 3 * text_y;
                applicationInterface.drawTextWithBackground(canvas, p, getContext().getResources().getString(R.string.processing), Color.LTGRAY, Color.BLACK, canvas.getWidth() / 2, text_base_y - pixels_offset_y);
            }
        }
        if (preview.supportsZoom() && show_zoom_pref) {
            float zoom_ratio = preview.getZoomRatio();
            // only show when actually zoomed in
            if (zoom_ratio > 1.0f + 1.0e-5f) {
                // Convert the dps to pixels, based on density scale
                // convert dps to pixels
                p.setTextSize(14 * scale + 0.5f);
                p.setTextAlign(Paint.Align.CENTER);
                applicationInterface.drawTextWithBackground(canvas, p, getContext().getResources().getString(R.string.zoom) + ": " + zoom_ratio + "x", Color.WHITE, Color.BLACK, canvas.getWidth() / 2, text_base_y - text_y, MyApplicationInterface.Alignment.ALIGNMENT_BOTTOM, ybounds_text, true);
            }
        }
    } else if (camera_controller == null) {
        /*if( MyDebug.LOG ) {
				Log.d(TAG, "no camera!");
				Log.d(TAG, "width " + canvas.getWidth() + " height " + canvas.getHeight());
			}*/
        p.setColor(Color.WHITE);
        // convert dps to pixels
        p.setTextSize(14 * scale + 0.5f);
        p.setTextAlign(Paint.Align.CENTER);
        // convert dps to pixels
        int pixels_offset = (int) (20 * scale + 0.5f);
        if (preview.hasPermissions()) {
            if (preview.openCameraFailed()) {
                canvas.drawText(getContext().getResources().getString(R.string.failed_to_open_camera_1), canvas.getWidth() / 2.0f, canvas.getHeight() / 2.0f, p);
                canvas.drawText(getContext().getResources().getString(R.string.failed_to_open_camera_2), canvas.getWidth() / 2.0f, canvas.getHeight() / 2.0f + pixels_offset, p);
                canvas.drawText(getContext().getResources().getString(R.string.failed_to_open_camera_3), canvas.getWidth() / 2.0f, canvas.getHeight() / 2.0f + 2 * pixels_offset, p);
            }
        } else {
            canvas.drawText(getContext().getResources().getString(R.string.no_permission), canvas.getWidth() / 2.0f, canvas.getHeight() / 2.0f, p);
        }
    // canvas.drawRect(0.0f, 0.0f, 100.0f, 100.0f, p);
    // canvas.drawRGB(255, 0, 0);
    // canvas.drawRect(0.0f, 0.0f, canvas.getWidth(), canvas.getHeight(), p);
    }
    // convert dps to pixels
    final int top_y = (int) (5 * scale + 0.5f);
    // convert dps to pixels
    int battery_x = (int) (5 * scale + 0.5f);
    int battery_y = top_y + (int) (5 * scale + 0.5f);
    // convert dps to pixels
    int battery_width = (int) (5 * scale + 0.5f);
    int battery_height = 4 * battery_width;
    if (ui_rotation == 90 || ui_rotation == 270) {
        int diff = canvas.getWidth() - canvas.getHeight();
        battery_x += diff / 2;
        battery_y -= diff / 2;
    }
    if (ui_rotation == 90) {
        battery_y = canvas.getHeight() - battery_y - battery_height;
    }
    if (ui_rotation == 180) {
        battery_x = canvas.getWidth() - battery_x - battery_width;
    }
    if (show_battery_pref) {
        if (!this.has_battery_frac || time_ms > this.last_battery_time + 60000) {
            // only check periodically - unclear if checking is costly in any way
            // note that it's fine to call registerReceiver repeatedly - we pass a null receiver, so this is fine as a "one shot" use
            Intent batteryStatus = main_activity.registerReceiver(null, battery_ifilter);
            int battery_level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int battery_scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            has_battery_frac = true;
            battery_frac = battery_level / (float) battery_scale;
            last_battery_time = time_ms;
            if (MyDebug.LOG)
                Log.d(TAG, "Battery status is " + battery_level + " / " + battery_scale + " : " + battery_frac);
        }
        // battery_frac = 0.2999f; // test
        boolean draw_battery = true;
        if (battery_frac <= 0.05f) {
            // flash icon at this low level
            draw_battery = (((time_ms / 1000)) % 2) == 0;
        }
        if (draw_battery) {
            // Green 500 or Red 500
            p.setColor(battery_frac > 0.15f ? Color.rgb(37, 155, 36) : Color.rgb(244, 67, 54));
            p.setStyle(Paint.Style.FILL);
            canvas.drawRect(battery_x, battery_y + (1.0f - battery_frac) * (battery_height - 2), battery_x + battery_width, battery_y + battery_height, p);
            if (battery_frac < 1.0f) {
                p.setColor(Color.BLACK);
                p.setAlpha(64);
                canvas.drawRect(battery_x, battery_y, battery_x + battery_width, battery_y + (1.0f - battery_frac) * (battery_height - 2), p);
            }
        }
    }
    onDrawInfoLines(canvas, top_y, time_ms);
    canvas.restore();
}
Also used : Rect(android.graphics.Rect) CameraController(net.sourceforge.opencamera.CameraController.CameraController) Preview(net.sourceforge.opencamera.Preview.Preview) Intent(android.content.Intent) Paint(android.graphics.Paint)

Example 13 with CameraController

use of net.sourceforge.opencamera.CameraController.CameraController in project OpenCamera by ageback.

the class DrawPreview method doThumbnailAnimation.

private void doThumbnailAnimation(Canvas canvas, long time_ms) {
    Preview preview = main_activity.getPreview();
    CameraController camera_controller = preview.getCameraController();
    // note, no need to check preferences here, as we do that when setting thumbnail_anim
    if (camera_controller != null && this.thumbnail_anim && last_thumbnail != null) {
        int ui_rotation = preview.getUIRotation();
        long time = time_ms - this.thumbnail_anim_start_ms;
        final long duration = 500;
        if (time > duration) {
            if (MyDebug.LOG)
                Log.d(TAG, "thumbnail_anim finished");
            this.thumbnail_anim = false;
        } else {
            thumbnail_anim_src_rect.left = 0;
            thumbnail_anim_src_rect.top = 0;
            thumbnail_anim_src_rect.right = last_thumbnail.getWidth();
            thumbnail_anim_src_rect.bottom = last_thumbnail.getHeight();
            View galleryButton = main_activity.findViewById(R.id.gallery);
            float alpha = ((float) time) / (float) duration;
            int st_x = canvas.getWidth() / 2;
            int st_y = canvas.getHeight() / 2;
            int nd_x = galleryButton.getLeft() + galleryButton.getWidth() / 2;
            int nd_y = galleryButton.getTop() + galleryButton.getHeight() / 2;
            int thumbnail_x = (int) ((1.0f - alpha) * st_x + alpha * nd_x);
            int thumbnail_y = (int) ((1.0f - alpha) * st_y + alpha * nd_y);
            float st_w = canvas.getWidth();
            float st_h = canvas.getHeight();
            float nd_w = galleryButton.getWidth();
            float nd_h = galleryButton.getHeight();
            // int thumbnail_w = (int)( (1.0f-alpha)*st_w + alpha*nd_w );
            // int thumbnail_h = (int)( (1.0f-alpha)*st_h + alpha*nd_h );
            float correction_w = st_w / nd_w - 1.0f;
            float correction_h = st_h / nd_h - 1.0f;
            int thumbnail_w = (int) (st_w / (1.0f + alpha * correction_w));
            int thumbnail_h = (int) (st_h / (1.0f + alpha * correction_h));
            thumbnail_anim_dst_rect.left = thumbnail_x - thumbnail_w / 2;
            thumbnail_anim_dst_rect.top = thumbnail_y - thumbnail_h / 2;
            thumbnail_anim_dst_rect.right = thumbnail_x + thumbnail_w / 2;
            thumbnail_anim_dst_rect.bottom = thumbnail_y + thumbnail_h / 2;
            // canvas.drawBitmap(this.thumbnail, thumbnail_anim_src_rect, thumbnail_anim_dst_rect, p);
            thumbnail_anim_matrix.setRectToRect(thumbnail_anim_src_rect, thumbnail_anim_dst_rect, Matrix.ScaleToFit.FILL);
            // thumbnail_anim_matrix.reset();
            if (ui_rotation == 90 || ui_rotation == 270) {
                float ratio = ((float) last_thumbnail.getWidth()) / (float) last_thumbnail.getHeight();
                thumbnail_anim_matrix.preScale(ratio, 1.0f / ratio, last_thumbnail.getWidth() / 2.0f, last_thumbnail.getHeight() / 2.0f);
            }
            thumbnail_anim_matrix.preRotate(ui_rotation, last_thumbnail.getWidth() / 2.0f, last_thumbnail.getHeight() / 2.0f);
            canvas.drawBitmap(last_thumbnail, thumbnail_anim_matrix, p);
        }
    }
}
Also used : CameraController(net.sourceforge.opencamera.CameraController.CameraController) Preview(net.sourceforge.opencamera.Preview.Preview) View(android.view.View) Paint(android.graphics.Paint)

Aggregations

CameraController (net.sourceforge.opencamera.CameraController.CameraController)13 Paint (android.graphics.Paint)10 Preview (net.sourceforge.opencamera.Preview.Preview)8 Point (android.graphics.Point)2 Rect (android.graphics.Rect)2 SuppressLint (android.annotation.SuppressLint)1 Activity (android.app.Activity)1 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Bitmap (android.graphics.Bitmap)1 Matrix (android.graphics.Matrix)1 CamcorderProfile (android.media.CamcorderProfile)1 AsyncTask (android.os.AsyncTask)1 Handler (android.os.Handler)1 Pair (android.util.Pair)1 View (android.view.View)1 ArrayList (java.util.ArrayList)1 CameraController1 (net.sourceforge.opencamera.CameraController.CameraController1)1 CameraController2 (net.sourceforge.opencamera.CameraController.CameraController2)1 CameraControllerException (net.sourceforge.opencamera.CameraController.CameraControllerException)1