Search in sources :

Example 1 with Preview

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

the class DrawPreview method onDrawInfoLines.

private void onDrawInfoLines(Canvas canvas, final int top_y, long time_ms) {
    Preview preview = main_activity.getPreview();
    CameraController camera_controller = preview.getCameraController();
    int ui_rotation = preview.getUIRotation();
    // set up text etc for the multiple lines of "info" (time, free mem, etc)
    // convert dps to pixels
    p.setTextSize(16 * scale + 0.5f);
    p.setTextAlign(Paint.Align.LEFT);
    // convert dps to pixels
    int location_x = (int) ((show_battery_pref ? 15 : 5) * scale + 0.5f);
    int location_y = top_y;
    // convert dps to pixels
    final int gap_y = (int) (0 * scale + 0.5f);
    if (ui_rotation == 90 || ui_rotation == 270) {
        int diff = canvas.getWidth() - canvas.getHeight();
        location_x += diff / 2;
        location_y -= diff / 2;
    }
    if (ui_rotation == 90) {
        location_y = canvas.getHeight() - location_y - (int) (20 * scale + 0.5f);
    }
    if (ui_rotation == 180) {
        location_x = canvas.getWidth() - location_x;
        p.setTextAlign(Paint.Align.RIGHT);
    }
    if (show_time_pref) {
        if (current_time_string == null || time_ms / 1000 > last_current_time_time / 1000) {
            // avoid creating a new calendar object every time
            if (calendar == null)
                calendar = Calendar.getInstance();
            else
                calendar.setTimeInMillis(time_ms);
            current_time_string = dateFormatTimeInstance.format(calendar.getTime());
            // current_time_string = DateUtils.formatDateTime(getContext(), c.getTimeInMillis(), DateUtils.FORMAT_SHOW_TIME);
            last_current_time_time = time_ms;
        }
        // int height = applicationInterface.drawTextWithBackground(canvas, p, current_time_string, Color.WHITE, Color.BLACK, location_x, location_y, MyApplicationInterface.Alignment.ALIGNMENT_TOP);
        if (text_bounds_time == null) {
            if (MyDebug.LOG)
                Log.d(TAG, "compute text_bounds_time");
            text_bounds_time = new Rect();
            String bounds_time_string = "00:00:00";
            p.getTextBounds(bounds_time_string, 0, bounds_time_string.length(), text_bounds_time);
        }
        int height = applicationInterface.drawTextWithBackground(canvas, p, current_time_string, Color.WHITE, Color.BLACK, location_x, location_y, MyApplicationInterface.Alignment.ALIGNMENT_TOP, null, true, text_bounds_time);
        height += gap_y;
        if (ui_rotation == 90) {
            location_y -= height;
        } else {
            location_y += height;
        }
    }
    if (camera_controller != null && show_free_memory_pref) {
        if (last_free_memory_time == 0 || time_ms > last_free_memory_time + 10000) {
            // don't call this too often, for UI performance
            long free_mb = main_activity.freeMemory();
            if (free_mb >= 0) {
                float new_free_memory_gb = free_mb / 1024.0f;
                if (MyDebug.LOG) {
                    Log.d(TAG, "free_memory_gb: " + free_memory_gb);
                    Log.d(TAG, "new_free_memory_gb: " + new_free_memory_gb);
                }
                if (Math.abs(new_free_memory_gb - free_memory_gb) > 0.001f) {
                    free_memory_gb = new_free_memory_gb;
                    free_memory_gb_string = decimalFormat.format(free_memory_gb) + getContext().getResources().getString(R.string.gb_abbreviation);
                }
            }
            // always set this, so that in case of free memory not being available, we aren't calling freeMemory() every frame
            last_free_memory_time = time_ms;
        }
        if (free_memory_gb >= 0.0f && free_memory_gb_string != null) {
            // int height = applicationInterface.drawTextWithBackground(canvas, p, free_memory_gb_string, Color.WHITE, Color.BLACK, location_x, location_y, MyApplicationInterface.Alignment.ALIGNMENT_TOP);
            if (text_bounds_free_memory == null) {
                if (MyDebug.LOG)
                    Log.d(TAG, "compute text_bounds_free_memory");
                text_bounds_free_memory = new Rect();
                p.getTextBounds(free_memory_gb_string, 0, free_memory_gb_string.length(), text_bounds_free_memory);
            }
            int height = applicationInterface.drawTextWithBackground(canvas, p, free_memory_gb_string, Color.WHITE, Color.BLACK, location_x, location_y, MyApplicationInterface.Alignment.ALIGNMENT_TOP, null, true, text_bounds_free_memory);
            height += gap_y;
            if (ui_rotation == 90) {
                location_y -= height;
            } else {
                location_y += height;
            }
        }
    }
    if (camera_controller != null && show_iso_pref) {
        if (iso_exposure_string == null || time_ms > last_iso_exposure_time + 500) {
            iso_exposure_string = "";
            if (camera_controller.captureResultHasIso()) {
                int iso = camera_controller.captureResultIso();
                if (iso_exposure_string.length() > 0)
                    iso_exposure_string += " ";
                iso_exposure_string += preview.getISOString(iso);
            }
            if (camera_controller.captureResultHasExposureTime()) {
                long exposure_time = camera_controller.captureResultExposureTime();
                if (iso_exposure_string.length() > 0)
                    iso_exposure_string += " ";
                iso_exposure_string += preview.getExposureTimeString(exposure_time);
            }
            /*if( camera_controller.captureResultHasFrameDuration() ) {
					long frame_duration = camera_controller.captureResultFrameDuration();
					if( iso_exposure_string.length() > 0 )
						iso_exposure_string += " ";
					iso_exposure_string += preview.getFrameDurationString(frame_duration);
				}*/
            last_iso_exposure_time = time_ms;
        }
        if (iso_exposure_string.length() > 0) {
            boolean is_scanning = false;
            if (camera_controller.captureResultIsAEScanning()) {
                // only show as scanning if in auto ISO mode (problem on Nexus 6 at least that if we're in manual ISO mode, after pausing and
                // resuming, the camera driver continually reports CONTROL_AE_STATE_SEARCHING)
                String value = sharedPreferences.getString(PreferenceKeys.ISOPreferenceKey, CameraController.ISO_DEFAULT);
                if (value.equals("auto")) {
                    is_scanning = true;
                }
            }
            // Yellow 500
            int text_color = Color.rgb(255, 235, 59);
            if (is_scanning) {
                // we only change the color if ae scanning is at least a certain time, otherwise we get a lot of flickering of the color
                if (ae_started_scanning_ms == -1) {
                    ae_started_scanning_ms = time_ms;
                } else if (time_ms - ae_started_scanning_ms > 500) {
                    // Red 500
                    text_color = Color.rgb(244, 67, 54);
                }
            } else {
                ae_started_scanning_ms = -1;
            }
            // can't cache the bounds rect, as the width may change significantly as the ISO or exposure values change
            int height = applicationInterface.drawTextWithBackground(canvas, p, iso_exposure_string, text_color, Color.BLACK, location_x, location_y, MyApplicationInterface.Alignment.ALIGNMENT_TOP, ybounds_text, true);
            height += gap_y;
            // been enabled, we'll never be able to display the on-screen ISO)
            if (ui_rotation == 90) {
                location_y -= height;
            } else {
                location_y += height;
            }
        }
    }
    if (camera_controller != null) {
        // padding to align with earlier text
        // convert dps to pixels
        final int flash_padding = (int) (1 * scale + 0.5f);
        int location_x2 = location_x - flash_padding;
        // convert dps to pixels
        final int icon_size = (int) (16 * scale + 0.5f);
        if (ui_rotation == 180) {
            location_x2 = location_x - icon_size + flash_padding;
        }
        if (store_location_pref) {
            icon_dest.set(location_x2, location_y, location_x2 + icon_size, location_y + icon_size);
            p.setStyle(Paint.Style.FILL);
            p.setColor(Color.BLACK);
            p.setAlpha(64);
            canvas.drawRect(icon_dest, p);
            p.setAlpha(255);
            if (applicationInterface.getLocation() != null) {
                canvas.drawBitmap(location_bitmap, null, icon_dest, p);
                int location_radius = icon_size / 10;
                int indicator_x = location_x2 + icon_size - (int) (location_radius * 1.5);
                int indicator_y = location_y + (int) (location_radius * 1.5);
                // Green 500 or Yellow 500
                p.setColor(applicationInterface.getLocation().getAccuracy() < 25.01f ? Color.rgb(37, 155, 36) : Color.rgb(255, 235, 59));
                canvas.drawCircle(indicator_x, indicator_y, location_radius, p);
            } else {
                canvas.drawBitmap(location_off_bitmap, null, icon_dest, p);
            }
            if (ui_rotation == 180) {
                location_x2 -= icon_size + flash_padding;
            } else {
                location_x2 += icon_size + flash_padding;
            }
        }
        // RAW not enabled in NR mode (see note in CameraController.takePictureBurst())
        if (is_raw_pref && // RAW can be enabled, even if it isn't available for this camera (e.g., user enables RAW for back camera, but then switches to front camera which doesn't support it)
        preview.supportsRaw() && photoMode != MyApplicationInterface.PhotoMode.HDR && photoMode != MyApplicationInterface.PhotoMode.ExpoBracketing && photoMode != MyApplicationInterface.PhotoMode.NoiseReduction) {
            icon_dest.set(location_x2, location_y, location_x2 + icon_size, location_y + icon_size);
            p.setStyle(Paint.Style.FILL);
            p.setColor(Color.BLACK);
            p.setAlpha(64);
            canvas.drawRect(icon_dest, p);
            p.setAlpha(255);
            canvas.drawBitmap(raw_bitmap, null, icon_dest, p);
            if (ui_rotation == 180) {
                location_x2 -= icon_size + flash_padding;
            } else {
                location_x2 += icon_size + flash_padding;
            }
        }
        if (is_face_detection_pref && preview.supportsFaceDetection()) {
            icon_dest.set(location_x2, location_y, location_x2 + icon_size, location_y + icon_size);
            p.setStyle(Paint.Style.FILL);
            p.setColor(Color.BLACK);
            p.setAlpha(64);
            canvas.drawRect(icon_dest, p);
            p.setAlpha(255);
            canvas.drawBitmap(face_detection_bitmap, null, icon_dest, p);
            if (ui_rotation == 180) {
                location_x2 -= icon_size + flash_padding;
            } else {
                location_x2 += icon_size + flash_padding;
            }
        }
        if (auto_stabilise_pref) {
            // auto-level is supported for photos taken in video mode
            icon_dest.set(location_x2, location_y, location_x2 + icon_size, location_y + icon_size);
            p.setStyle(Paint.Style.FILL);
            p.setColor(Color.BLACK);
            p.setAlpha(64);
            canvas.drawRect(icon_dest, p);
            p.setAlpha(255);
            canvas.drawBitmap(auto_stabilise_bitmap, null, icon_dest, p);
            if (ui_rotation == 180) {
                location_x2 -= icon_size + flash_padding;
            } else {
                location_x2 += icon_size + flash_padding;
            }
        }
        if ((photoMode == MyApplicationInterface.PhotoMode.DRO || photoMode == MyApplicationInterface.PhotoMode.HDR || photoMode == MyApplicationInterface.PhotoMode.ExpoBracketing || photoMode == MyApplicationInterface.PhotoMode.FastBurst || photoMode == MyApplicationInterface.PhotoMode.NoiseReduction) && !applicationInterface.isVideoPref()) {
            // these photo modes not supported for video mode
            icon_dest.set(location_x2, location_y, location_x2 + icon_size, location_y + icon_size);
            p.setStyle(Paint.Style.FILL);
            p.setColor(Color.BLACK);
            p.setAlpha(64);
            canvas.drawRect(icon_dest, p);
            p.setAlpha(255);
            Bitmap bitmap = photoMode == MyApplicationInterface.PhotoMode.DRO ? dro_bitmap : photoMode == MyApplicationInterface.PhotoMode.HDR ? hdr_bitmap : photoMode == MyApplicationInterface.PhotoMode.ExpoBracketing ? expo_bitmap : photoMode == MyApplicationInterface.PhotoMode.FastBurst ? burst_bitmap : photoMode == MyApplicationInterface.PhotoMode.NoiseReduction ? nr_bitmap : null;
            if (bitmap != null) {
                canvas.drawBitmap(bitmap, null, icon_dest, p);
                if (ui_rotation == 180) {
                    location_x2 -= icon_size + flash_padding;
                } else {
                    location_x2 += icon_size + flash_padding;
                }
            }
        }
        // but it isn't supported in RAW-only mode
        if (has_stamp_pref && !(is_raw_only_pref && preview.supportsRaw())) {
            icon_dest.set(location_x2, location_y, location_x2 + icon_size, location_y + icon_size);
            p.setStyle(Paint.Style.FILL);
            p.setColor(Color.BLACK);
            p.setAlpha(64);
            canvas.drawRect(icon_dest, p);
            p.setAlpha(255);
            canvas.drawBitmap(photostamp_bitmap, null, icon_dest, p);
            if (ui_rotation == 180) {
                location_x2 -= icon_size + flash_padding;
            } else {
                location_x2 += icon_size + flash_padding;
            }
        }
        if (!is_audio_enabled_pref && applicationInterface.isVideoPref()) {
            icon_dest.set(location_x2, location_y, location_x2 + icon_size, location_y + icon_size);
            p.setStyle(Paint.Style.FILL);
            p.setColor(Color.BLACK);
            p.setAlpha(64);
            canvas.drawRect(icon_dest, p);
            p.setAlpha(255);
            canvas.drawBitmap(audio_disabled_bitmap, null, icon_dest, p);
            if (ui_rotation == 180) {
                location_x2 -= icon_size + flash_padding;
            } else {
                location_x2 += icon_size + flash_padding;
            }
        }
        if (is_high_speed && applicationInterface.isVideoPref()) {
            icon_dest.set(location_x2, location_y, location_x2 + icon_size, location_y + icon_size);
            p.setStyle(Paint.Style.FILL);
            p.setColor(Color.BLACK);
            p.setAlpha(64);
            canvas.drawRect(icon_dest, p);
            p.setAlpha(255);
            canvas.drawBitmap(high_speed_fps_bitmap, null, icon_dest, p);
            if (ui_rotation == 180) {
                location_x2 -= icon_size + flash_padding;
            } else {
                location_x2 += icon_size + flash_padding;
            }
        }
        String flash_value = preview.getCurrentFlashValue();
        // note, flash_frontscreen_auto not yet support for the flash symbol (as camera_controller.needsFlash() only returns info on the built-in actual flash, not frontscreen flash)
        if (flash_value != null && (flash_value.equals("flash_on") || flash_value.equals("flash_red_eye") || (flash_value.equals("flash_auto") && camera_controller.needsFlash())) && !applicationInterface.isVideoPref()) {
            // flash-indicator not supported for photos taken in video mode
            if (needs_flash_time != -1) {
                final long fade_ms = 500;
                float alpha = (time_ms - needs_flash_time) / (float) fade_ms;
                if (time_ms - needs_flash_time >= fade_ms)
                    alpha = 1.0f;
                icon_dest.set(location_x2, location_y, location_x2 + icon_size, location_y + icon_size);
                /*if( MyDebug.LOG )
						Log.d(TAG, "alpha: " + alpha);*/
                p.setStyle(Paint.Style.FILL);
                p.setColor(Color.BLACK);
                p.setAlpha((int) (64 * alpha));
                canvas.drawRect(icon_dest, p);
                p.setAlpha((int) (255 * alpha));
                canvas.drawBitmap(flash_bitmap, null, icon_dest, p);
            } else {
                needs_flash_time = time_ms;
            }
        } else {
            needs_flash_time = -1;
        }
    }
}
Also used : Rect(android.graphics.Rect) Bitmap(android.graphics.Bitmap) CameraController(net.sourceforge.opencamera.CameraController.CameraController) Preview(net.sourceforge.opencamera.Preview.Preview) Paint(android.graphics.Paint)

Example 2 with Preview

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

the class DrawPreview method drawAngleLines.

private void drawAngleLines(Canvas canvas) {
    Preview preview = main_activity.getPreview();
    CameraController camera_controller = preview.getCameraController();
    boolean has_level_angle = preview.hasLevelAngle();
    if (camera_controller != null && !preview.isPreviewPaused() && has_level_angle && (show_angle_line_pref || show_pitch_lines_pref || show_geo_direction_lines_pref)) {
        int ui_rotation = preview.getUIRotation();
        double level_angle = preview.getLevelAngle();
        boolean has_pitch_angle = preview.hasPitchAngle();
        double pitch_angle = preview.getPitchAngle();
        boolean has_geo_direction = preview.hasGeoDirection();
        double geo_direction = preview.getGeoDirection();
        // n.b., must draw this without the standard canvas rotation
        int radius_dps = (ui_rotation == 90 || ui_rotation == 270) ? 60 : 80;
        // convert dps to pixels
        int radius = (int) (radius_dps * scale + 0.5f);
        double angle = -preview.getOrigLevelAngle();
        // see http://android-developers.blogspot.co.uk/2010/09/one-screen-turn-deserves-another.html
        int rotation = main_activity.getWindowManager().getDefaultDisplay().getRotation();
        switch(rotation) {
            case Surface.ROTATION_90:
            case Surface.ROTATION_270:
                angle -= 90.0;
                break;
            case Surface.ROTATION_0:
            case Surface.ROTATION_180:
            default:
                break;
        }
        /*if( MyDebug.LOG ) {
				Log.d(TAG, "orig_level_angle: " + preview.getOrigLevelAngle());
				Log.d(TAG, "angle: " + angle);
			}*/
        int cx = canvas.getWidth() / 2;
        int cy = canvas.getHeight() / 2;
        boolean is_level = false;
        if (Math.abs(level_angle) <= close_level_angle) {
            // n.b., use level_angle, not angle or orig_level_angle
            is_level = true;
        }
        if (is_level) {
            radius = (int) (radius * 1.2);
        }
        canvas.save();
        canvas.rotate((float) angle, cx, cy);
        final int line_alpha = 96;
        // convert dps to pixels
        float hthickness = (0.5f * scale + 0.5f);
        p.setStyle(Paint.Style.FILL);
        if (show_angle_line_pref) {
            // draw outline
            p.setColor(Color.BLACK);
            p.setAlpha(64);
            // can't use drawRoundRect(left, top, right, bottom, ...) as that requires API 21
            draw_rect.set(cx - radius - hthickness, cy - 2 * hthickness, cx + radius + hthickness, cy + 2 * hthickness);
            canvas.drawRoundRect(draw_rect, 2 * hthickness, 2 * hthickness, p);
            // draw the vertical crossbar
            draw_rect.set(cx - 2 * hthickness, cy - radius / 2 - hthickness, cx + 2 * hthickness, cy + radius / 2 + hthickness);
            canvas.drawRoundRect(draw_rect, hthickness, hthickness, p);
            // draw inner portion
            if (is_level) {
                p.setColor(angle_highlight_color_pref);
            } else {
                p.setColor(Color.WHITE);
            }
            p.setAlpha(line_alpha);
            draw_rect.set(cx - radius, cy - hthickness, cx + radius, cy + hthickness);
            canvas.drawRoundRect(draw_rect, hthickness, hthickness, p);
            // draw the vertical crossbar
            draw_rect.set(cx - hthickness, cy - radius / 2, cx + hthickness, cy + radius / 2);
            canvas.drawRoundRect(draw_rect, hthickness, hthickness, p);
            if (is_level) {
                // draw a second line
                p.setColor(Color.BLACK);
                p.setAlpha(64);
                draw_rect.set(cx - radius - hthickness, cy - 7 * hthickness, cx + radius + hthickness, cy - 3 * hthickness);
                canvas.drawRoundRect(draw_rect, 2 * hthickness, 2 * hthickness, p);
                p.setColor(angle_highlight_color_pref);
                p.setAlpha(line_alpha);
                draw_rect.set(cx - radius, cy - 6 * hthickness, cx + radius, cy - 4 * hthickness);
                canvas.drawRoundRect(draw_rect, hthickness, hthickness, p);
            }
        }
        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)))));
        /*if( MyDebug.LOG ) {
				Log.d(TAG, "camera_angle_x: " + camera_angle_x);
				Log.d(TAG, "camera_angle_y: " + camera_angle_y);
				Log.d(TAG, "angle_scale_x: " + angle_scale_x);
				Log.d(TAG, "angle_scale_y: " + angle_scale_y);
				Log.d(TAG, "angle_scale_x/scale: " + angle_scale_x/scale);
				Log.d(TAG, "angle_scale_y/scale: " + angle_scale_y/scale);
			}*/
        /*if( MyDebug.LOG ) {
				Log.d(TAG, "has_pitch_angle?: " + has_pitch_angle);
				Log.d(TAG, "show_pitch_lines?: " + show_pitch_lines);
			}*/
        float angle_scale = (float) Math.sqrt(angle_scale_x * angle_scale_x + angle_scale_y * angle_scale_y);
        angle_scale *= preview.getZoomRatio();
        if (has_pitch_angle && show_pitch_lines_pref) {
            int pitch_radius_dps = (ui_rotation == 90 || ui_rotation == 270) ? 100 : 80;
            // convert dps to pixels
            int pitch_radius = (int) (pitch_radius_dps * scale + 0.5f);
            int angle_step = 10;
            if (preview.getZoomRatio() >= 2.0f)
                angle_step = 5;
            for (int latitude_angle = -90; latitude_angle <= 90; latitude_angle += angle_step) {
                double this_angle = pitch_angle - latitude_angle;
                if (Math.abs(this_angle) < 90.0) {
                    // angle_scale is already in pixels rather than dps
                    float pitch_distance = angle_scale * (float) Math.tan(Math.toRadians(this_angle));
                    /*if( MyDebug.LOG ) {
							Log.d(TAG, "pitch_angle: " + pitch_angle);
							Log.d(TAG, "pitch_distance_dp: " + pitch_distance_dp);
						}*/
                    // draw outline
                    p.setColor(Color.BLACK);
                    p.setAlpha(64);
                    // can't use drawRoundRect(left, top, right, bottom, ...) as that requires API 21
                    draw_rect.set(cx - pitch_radius - hthickness, cy + pitch_distance - 2 * hthickness, cx + pitch_radius + hthickness, cy + pitch_distance + 2 * hthickness);
                    canvas.drawRoundRect(draw_rect, 2 * hthickness, 2 * hthickness, p);
                    // draw inner portion
                    p.setColor(Color.WHITE);
                    p.setTextAlign(Paint.Align.LEFT);
                    if (latitude_angle == 0 && Math.abs(pitch_angle) < 1.0) {
                        p.setAlpha(255);
                    } else {
                        p.setAlpha(line_alpha);
                    }
                    draw_rect.set(cx - pitch_radius, cy + pitch_distance - hthickness, cx + pitch_radius, cy + pitch_distance + hthickness);
                    canvas.drawRoundRect(draw_rect, hthickness, hthickness, p);
                    // draw pitch angle indicator
                    applicationInterface.drawTextWithBackground(canvas, p, "" + latitude_angle + "\u00B0", p.getColor(), Color.BLACK, (int) (cx + pitch_radius + 4 * hthickness), (int) (cy + pitch_distance - 2 * hthickness), MyApplicationInterface.Alignment.ALIGNMENT_CENTRE);
                }
            }
        }
        if (has_geo_direction && has_pitch_angle && show_geo_direction_lines_pref) {
            int geo_radius_dps = (ui_rotation == 90 || ui_rotation == 270) ? 80 : 100;
            // convert dps to pixels
            int geo_radius = (int) (geo_radius_dps * scale + 0.5f);
            float geo_angle = (float) Math.toDegrees(geo_direction);
            int angle_step = 10;
            if (preview.getZoomRatio() >= 2.0f)
                angle_step = 5;
            for (int longitude_angle = 0; longitude_angle < 360; longitude_angle += angle_step) {
                double this_angle = longitude_angle - geo_angle;
                // normalise to be in interval [0, 360)
                while (this_angle >= 360.0) this_angle -= 360.0;
                while (this_angle < -360.0) this_angle += 360.0;
                // pick shortest angle
                if (this_angle > 180.0)
                    this_angle = -(360.0 - this_angle);
                if (Math.abs(this_angle) < 90.0) {
                    /*if( MyDebug.LOG ) {
							Log.d(TAG, "this_angle is now: " + this_angle);
						}*/
                    // angle_scale is already in pixels rather than dps
                    float geo_distance = angle_scale * (float) Math.tan(Math.toRadians(this_angle));
                    // draw outline
                    p.setColor(Color.BLACK);
                    p.setAlpha(64);
                    // can't use drawRoundRect(left, top, right, bottom, ...) as that requires API 21
                    draw_rect.set(cx + geo_distance - 2 * hthickness, cy - geo_radius - hthickness, cx + geo_distance + 2 * hthickness, cy + geo_radius + hthickness);
                    canvas.drawRoundRect(draw_rect, 2 * hthickness, 2 * hthickness, p);
                    // draw inner portion
                    p.setColor(Color.WHITE);
                    p.setTextAlign(Paint.Align.CENTER);
                    p.setAlpha(line_alpha);
                    draw_rect.set(cx + geo_distance - hthickness, cy - geo_radius, cx + geo_distance + hthickness, cy + geo_radius);
                    canvas.drawRoundRect(draw_rect, hthickness, hthickness, p);
                    // draw geo direction angle indicator
                    applicationInterface.drawTextWithBackground(canvas, p, "" + longitude_angle + "\u00B0", p.getColor(), Color.BLACK, (int) (cx + geo_distance), (int) (cy - geo_radius - 4 * hthickness), MyApplicationInterface.Alignment.ALIGNMENT_BOTTOM);
                }
            }
        }
        p.setAlpha(255);
        // reset
        p.setStyle(Paint.Style.FILL);
        canvas.restore();
    }
}
Also used : CameraController(net.sourceforge.opencamera.CameraController.CameraController) Preview(net.sourceforge.opencamera.Preview.Preview) Paint(android.graphics.Paint)

Example 3 with Preview

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

the class DrawPreview method doFocusAnimation.

private void doFocusAnimation(Canvas canvas, long time_ms) {
    Preview preview = main_activity.getPreview();
    CameraController camera_controller = preview.getCameraController();
    if (camera_controller != null && continuous_focus_moving && !taking_picture) {
        // we don't display the continuous focusing animation when taking a photo - and can also give the impression of having
        // frozen if we pause because the image saver queue is full
        long dt = time_ms - continuous_focus_moving_ms;
        final long length = 1000;
        /*if( MyDebug.LOG )
				Log.d(TAG, "continuous focus moving, dt: " + dt);*/
        if (dt <= length) {
            float frac = ((float) dt) / (float) length;
            float pos_x = canvas.getWidth() / 2.0f;
            float pos_y = canvas.getHeight() / 2.0f;
            // convert dps to pixels
            float min_radius = (40 * scale + 0.5f);
            // convert dps to pixels
            float max_radius = (60 * scale + 0.5f);
            float radius;
            if (frac < 0.5f) {
                float alpha = frac * 2.0f;
                radius = (1.0f - alpha) * min_radius + alpha * max_radius;
            } else {
                float alpha = (frac - 0.5f) * 2.0f;
                radius = (1.0f - alpha) * max_radius + alpha * min_radius;
            }
            /*if( MyDebug.LOG ) {
					Log.d(TAG, "dt: " + dt);
					Log.d(TAG, "radius: " + radius);
				}*/
            p.setColor(Color.WHITE);
            p.setStyle(Paint.Style.STROKE);
            p.setStrokeWidth(stroke_width);
            canvas.drawCircle(pos_x, pos_y, radius, p);
            // reset
            p.setStyle(Paint.Style.FILL);
        } else {
            clearContinuousFocusMove();
        }
    }
    if (preview.isFocusWaiting() || preview.isFocusRecentSuccess() || preview.isFocusRecentFailure()) {
        long time_since_focus_started = preview.timeSinceStartedAutoFocus();
        // convert dps to pixels
        float min_radius = (40 * scale + 0.5f);
        // convert dps to pixels
        float max_radius = (45 * scale + 0.5f);
        float radius = min_radius;
        if (time_since_focus_started > 0) {
            final long length = 500;
            float frac = ((float) time_since_focus_started) / (float) length;
            if (frac > 1.0f)
                frac = 1.0f;
            if (frac < 0.5f) {
                float alpha = frac * 2.0f;
                radius = (1.0f - alpha) * min_radius + alpha * max_radius;
            } else {
                float alpha = (frac - 0.5f) * 2.0f;
                radius = (1.0f - alpha) * max_radius + alpha * min_radius;
            }
        }
        int size = (int) radius;
        if (preview.isFocusRecentSuccess())
            // Green A400
            p.setColor(Color.rgb(20, 231, 21));
        else if (preview.isFocusRecentFailure())
            // Red 500
            p.setColor(Color.rgb(244, 67, 54));
        else
            p.setColor(Color.WHITE);
        p.setStyle(Paint.Style.STROKE);
        p.setStrokeWidth(stroke_width);
        int pos_x;
        int pos_y;
        if (preview.hasFocusArea()) {
            Pair<Integer, Integer> focus_pos = preview.getFocusPos();
            pos_x = focus_pos.first;
            pos_y = focus_pos.second;
        } else {
            pos_x = canvas.getWidth() / 2;
            pos_y = canvas.getHeight() / 2;
        }
        float frac = 0.5f;
        // horizontal strokes
        canvas.drawLine(pos_x - size, pos_y - size, pos_x - frac * size, pos_y - size, p);
        canvas.drawLine(pos_x + frac * size, pos_y - size, pos_x + size, pos_y - size, p);
        canvas.drawLine(pos_x - size, pos_y + size, pos_x - frac * size, pos_y + size, p);
        canvas.drawLine(pos_x + frac * size, pos_y + size, pos_x + size, pos_y + size, p);
        // vertical strokes
        canvas.drawLine(pos_x - size, pos_y - size, pos_x - size, pos_y - frac * size, p);
        canvas.drawLine(pos_x - size, pos_y + frac * size, pos_x - size, pos_y + size, p);
        canvas.drawLine(pos_x + size, pos_y - size, pos_x + size, pos_y - frac * size, p);
        canvas.drawLine(pos_x + size, pos_y + frac * size, pos_x + size, pos_y + size, 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 4 with Preview

use of net.sourceforge.opencamera.Preview.Preview 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);
            }
        }
    }
}
Also used : CameraController(net.sourceforge.opencamera.CameraController.CameraController) Preview(net.sourceforge.opencamera.Preview.Preview) GyroSensor(net.sourceforge.opencamera.GyroSensor) Paint(android.graphics.Paint)

Example 5 with Preview

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

the class MainUI method updateSelectedISOButton.

/**
 * If the exposure panel is open, updates the selected ISO button to match the current ISO value,
 *  if a continuous range of ISO values are supported by the camera.
 */
public void updateSelectedISOButton() {
    if (MyDebug.LOG)
        Log.d(TAG, "updateSelectedISOButton");
    Preview preview = main_activity.getPreview();
    if (preview.supportsISORange() && isExposureUIOpen()) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(main_activity);
        String current_iso = sharedPreferences.getString(PreferenceKeys.ISOPreferenceKey, CameraController.ISO_DEFAULT);
        // if the manual ISO value isn't one of the "preset" values, then instead highlight the manual ISO icon
        if (MyDebug.LOG)
            Log.d(TAG, "current_iso: " + current_iso);
        boolean found = false;
        for (View view : iso_buttons) {
            Button button = (Button) view;
            if (MyDebug.LOG)
                Log.d(TAG, "button: " + button.getText());
            String button_text = "" + button.getText();
            if (button_text.contains(current_iso)) {
                PopupView.setButtonSelected(button, true);
                found = true;
            } else {
                PopupView.setButtonSelected(button, false);
            }
        }
        if (!found && !current_iso.equals(CameraController.ISO_DEFAULT)) {
            if (MyDebug.LOG)
                Log.d(TAG, "must be manual");
            if (iso_button_manual_index >= 0 && iso_button_manual_index < iso_buttons.size()) {
                Button button = (Button) iso_buttons.get(iso_button_manual_index);
                PopupView.setButtonSelected(button, true);
            }
        }
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) ImageButton(android.widget.ImageButton) Button(android.widget.Button) Preview(net.sourceforge.opencamera.Preview.Preview) View(android.view.View)

Aggregations

Preview (net.sourceforge.opencamera.Preview.Preview)14 Paint (android.graphics.Paint)10 CameraController (net.sourceforge.opencamera.CameraController.CameraController)8 SharedPreferences (android.content.SharedPreferences)4 View (android.view.View)4 Intent (android.content.Intent)3 Rect (android.graphics.Rect)2 DrawPreview (net.sourceforge.opencamera.UI.DrawPreview)2 SuppressLint (android.annotation.SuppressLint)1 ActivityManager (android.app.ActivityManager)1 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 PackageInfo (android.content.pm.PackageInfo)1 Bitmap (android.graphics.Bitmap)1 Uri (android.net.Uri)1 Handler (android.os.Handler)1 RecognizerIntent (android.speech.RecognizerIntent)1 TextToSpeech (android.speech.tts.TextToSpeech)1 GestureDetector (android.view.GestureDetector)1 OrientationEventListener (android.view.OrientationEventListener)1