How to display an image with correct physical dimension on screen (1cm in real = 1cm on screen)
-
hi there,
I am trying to figure out how to calculate the scale factor to display an given image
in a GraphicsView that 1cm of the image will be 1cm on screen.
Or in other words hat the image is display in his real size.Of course I need to take into considerations:
Image density (resolution)
Physical DPI of the monitorIn order to determine what Qt is providing I wrote this small code.
Below is the output from the debugger when I load an image with 20cm at 150 dpi density.Would somebody give me the hint wich Parameters I need to calculate the scale factor?
ui->graphicsView->resetTransform(); int imageResolution = 150; QScreen* screen = QGuiApplication::primaryScreen(); double logicalDpiX = screen->logicalDotsPerInchX(); double logicalDpiY = screen->logicalDotsPerInchY(); double physicalDpiX = screen->physicalDotsPerInchX(); double physicalDpiY = screen->physicalDotsPerInchY(); double desiredImageSizeXmm = m_processedImage.width() / imageResolution * 2.54; double desiredImageSizeYmm = m_processedImage.height() / imageResolution * 2.54; double imageSizeXmm = m_processedImage.widthMM(); double imageSizeYmm = m_processedImage.heightMM(); double logicalImageSizeX = m_processedImage.logicalDpiX(); double logicalImageSizeY = m_processedImage.logicalDpiY(); double imageSizeX = m_processedImage.width(); double imageSizeY = m_processedImage.height(); //float scaleFactorX = ???; //float scaleFactorY = ???; //float scaleFactor = qMin(scaleFactorX, scaleFactorY); ui->graphicsView->scale(scaleFactor, scaleFactor); ui->graphicsView->viewport()->rect().center();
Debugger Output:
[statics]
desiredImageSizeXmm 20.32 double
desiredImageSizeYmm 15.24 double
imageSizeX 1204.0 double
imageSizeXmm 425.0 double
imageSizeY 963.0 double
imageSizeYmm 340.0 double
logicalDpiX 72.0 double
logicalDpiY 72.0 double
logicalImageSizeX 72.0 double
logicalImageSizeY 72.0 double
physicalDpiX 109.00000163701577 double
physicalDpiY 109.00000163701577 double -
@ademmler Hi
I did this in some old project, please adapt to you needs
int DpiManager::getPixelsFromCentimeters(const double ¢imeters_) { // 1 inch = 2.54 centimeters // x inch = n centimeters // x inchs = n / 2.54 // double inchs = centimeters_ / 2.54; // m_physicalDotsPerInch = 1 inch // x dots = n inchs // x = n inchs * m_physicalDotsPerInch; return int(centimeters_ / 2.54 * m_physicalDotsPerInch); //m_physicalDotsPerInch is a cached property //m_physicalDotsPerInch = QGuiApplication::primaryScreen()->physicalDotsPerInch(); }
In qml I do this:
property real mm: Screen.pixelDensity property real my1centimerterSize: 10 * mm
-
hi there,
I am trying to figure out how to calculate the scale factor to display an given image
in a GraphicsView that 1cm of the image will be 1cm on screen.
Or in other words hat the image is display in his real size.Of course I need to take into considerations:
Image density (resolution)
Physical DPI of the monitorIn order to determine what Qt is providing I wrote this small code.
Below is the output from the debugger when I load an image with 20cm at 150 dpi density.Would somebody give me the hint wich Parameters I need to calculate the scale factor?
ui->graphicsView->resetTransform(); int imageResolution = 150; QScreen* screen = QGuiApplication::primaryScreen(); double logicalDpiX = screen->logicalDotsPerInchX(); double logicalDpiY = screen->logicalDotsPerInchY(); double physicalDpiX = screen->physicalDotsPerInchX(); double physicalDpiY = screen->physicalDotsPerInchY(); double desiredImageSizeXmm = m_processedImage.width() / imageResolution * 2.54; double desiredImageSizeYmm = m_processedImage.height() / imageResolution * 2.54; double imageSizeXmm = m_processedImage.widthMM(); double imageSizeYmm = m_processedImage.heightMM(); double logicalImageSizeX = m_processedImage.logicalDpiX(); double logicalImageSizeY = m_processedImage.logicalDpiY(); double imageSizeX = m_processedImage.width(); double imageSizeY = m_processedImage.height(); //float scaleFactorX = ???; //float scaleFactorY = ???; //float scaleFactor = qMin(scaleFactorX, scaleFactorY); ui->graphicsView->scale(scaleFactor, scaleFactor); ui->graphicsView->viewport()->rect().center();
Debugger Output:
[statics]
desiredImageSizeXmm 20.32 double
desiredImageSizeYmm 15.24 double
imageSizeX 1204.0 double
imageSizeXmm 425.0 double
imageSizeY 963.0 double
imageSizeYmm 340.0 double
logicalDpiX 72.0 double
logicalDpiY 72.0 double
logicalImageSizeX 72.0 double
logicalImageSizeY 72.0 double
physicalDpiX 109.00000163701577 double
physicalDpiY 109.00000163701577 double@ademmler Hi
I did this in some old project, please adapt to you needs
int DpiManager::getPixelsFromCentimeters(const double ¢imeters_) { // 1 inch = 2.54 centimeters // x inch = n centimeters // x inchs = n / 2.54 // double inchs = centimeters_ / 2.54; // m_physicalDotsPerInch = 1 inch // x dots = n inchs // x = n inchs * m_physicalDotsPerInch; return int(centimeters_ / 2.54 * m_physicalDotsPerInch); //m_physicalDotsPerInch is a cached property //m_physicalDotsPerInch = QGuiApplication::primaryScreen()->physicalDotsPerInch(); }
In qml I do this:
property real mm: Screen.pixelDensity property real my1centimerterSize: 10 * mm
-
@ademmler Hi
I did this in some old project, please adapt to you needs
int DpiManager::getPixelsFromCentimeters(const double ¢imeters_) { // 1 inch = 2.54 centimeters // x inch = n centimeters // x inchs = n / 2.54 // double inchs = centimeters_ / 2.54; // m_physicalDotsPerInch = 1 inch // x dots = n inchs // x = n inchs * m_physicalDotsPerInch; return int(centimeters_ / 2.54 * m_physicalDotsPerInch); //m_physicalDotsPerInch is a cached property //m_physicalDotsPerInch = QGuiApplication::primaryScreen()->physicalDotsPerInch(); }
In qml I do this:
property real mm: Screen.pixelDensity property real my1centimerterSize: 10 * mm
-
A ademmler has marked this topic as solved on