-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathShowImage.cpp
91 lines (77 loc) · 3.36 KB
/
ShowImage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "mainwindow.h"
#include "ui_mainwindow.h"
void MainWindow::OnShowImageCliked()
{
//QString filename;
QString jpg_str=".jpg";
QString png_str=".png";
QString bmp_str=".bmp";
filename=QFileDialog::getOpenFileName(this,
tr("Open image"), "",
tr("Images (*.png *.bmp *.jpg *.tif *.GIF )"));
if(filename.isEmpty())
{
return;
}
else{
QImage* img=new QImage;
img->load(filename);
//image read
//vtkSmartPointer<vtkPNGReader> imageReader_1 =
// vtkSmartPointer<vtkPNGReader>::New();
//vtkSmartPointer<vtkPNGReader> imageReader_1 =
// vtkSmartPointer<vtkPNGReader>::New();
vtkSmartPointer<vtkBMPReader>imageReader_3 =
vtkSmartPointer<vtkBMPReader>::New();
imageReader_3->SetFileName((const char*)filename.toStdString().c_str());
imageReader_3->Update();
vtkSmartPointer<vtkJPEGReader>imageReader_2 =
vtkSmartPointer<vtkJPEGReader>::New();
imageReader_2->SetFileName((const char*)filename.toStdString().c_str());
imageReader_2->Update();
vtkSmartPointer<vtkPNGReader> imageReader_1 =
vtkSmartPointer<vtkPNGReader>::New();
// imageReader_1->SetFileName((const char*)filename.toStdString().c_str());
imageReader_1->SetFileName((const char*)filename.toStdString().c_str());
imageReader_1->Update();
//mapper
vtkSmartPointer<vtkImageMapper> imageMapper_1 =
vtkSmartPointer<vtkImageMapper>::New();
//if(filename.right(4).compare(jpg_str)==0)
imageMapper_1->SetInputData(imageReader_2->GetOutput());
//else
// imageMapper_1->SetInputData(imageReader_1->GetOutput());
imageMapper_1->SetColorWindow(255);
imageMapper_1->SetColorLevel(127.5);
//imageMapper_1->GetInput();
//actor
vtkSmartPointer<vtkImageActor> imageActor_1 =
vtkSmartPointer<vtkImageActor>::New();
//vtkSmartPointer<vtkActor2D> imageActor_1 = vtkSmartPointer<vtkActor2D>::New();
//imageActor_1->SetMapper(imageMapper_1);
if(filename.right(4).compare(png_str)==0)
imageActor_1->GetMapper()->SetInputData(imageReader_1->GetOutput());
else if(filename.right(4).compare(jpg_str)==0)
imageActor_1->GetMapper()->SetInputData(imageReader_2->GetOutput());
else if(filename.right(4).compare(bmp_str)==0)
imageActor_1->GetMapper()->SetInputData(imageReader_3->GetOutput());
//imageActor_1->SetPosition(20,20);
//render
vtkSmartPointer<vtkRenderer> imageRenderer_1 =
vtkSmartPointer<vtkRenderer>::New();
imageRenderer_1->AddActor(imageActor_1);
imageRenderer_1->ResetCamera();
//imageRenderer_1->SetViewPoint();
//show
ui->qvtkWidget_1->GetRenderWindow()->AddRenderer(imageRenderer_1);
ui->qvtkWidget_1->GetRenderWindow()->Render();
if(! ( img->load(filename) ) ) //加载图像
{
QMessageBox::information(this,
tr("打开图像失败"),
tr("打开图像失败!"));
delete img;
return;
}
}
}