小编Ily*_*lya的帖子

C++:Hbitmap/BITMAP到.bmp文件中

好吧,整个故事是,我试图在C++中使用Leptonica + Tesseract OCR截取屏幕截图,将其保存为*.bmp文件,然后将其加载回OCR.我不需要经常这样做,但由于我似乎无法将屏幕截图数据直接复制到Leptonica PIX结构中,我需要先将其保存到文件中......实际上,最好是解决这个问题.

这是我在网上找到的一些代码,试图帮助我.

屏幕上限:

HBITMAP ScreenCapture(){
  int width=100;
  int height=100;
  // get the device context of the screen
  HDC hScreenDC = CreateDC(L"DISPLAY", NULL, NULL, NULL);     
  // and a device context to put it in
  HDC hMemoryDC = CreateCompatibleDC(hScreenDC);

  int x = GetDeviceCaps(hScreenDC, HORZRES);
  int y = GetDeviceCaps(hScreenDC, VERTRES);

  // maybe worth checking these are positive values
  HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, x, y);

  // get a new bitmap
  HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemoryDC, hBitmap);

  BitBlt(hMemoryDC, 0, 0, width, height, …
Run Code Online (Sandbox Code Playgroud)

c++ dib hbitmap leptonica

2
推荐指数
1
解决办法
2万
查看次数

C++中灵活的枚举

我如何组织枚举,以便每个枚举条目都有参数,例如我在Haxe中可以做的,例如:

enum GraphicAct {
  ClearScreen; 
  MoveTo(x:Float, y:Float);
  LineTo(x:Float, y:Float);
  FillColor(color:Int);
  EndFill;
}

function main(){
  var actions:Array<GraphicAct>;
  actions.push(ClearScreen);
  actions.push(FillColor(0xaaffff));
  actions.push(MoveTo(100, 100));
  actions.push(LineTo(200, 100));
  actions.push(LineTo(100, 200));
  actions.push(EndFill);


  for(act in actions){
    switch(act){
       case ClearScreen: // do clear screen here...
       case MoveTo(x, y): // move position
       case LineTo(x, y): // move position
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

据我所知,C++仅支持没有参数的枚举条目,如"ClearScreen"和"EndFill",但在这种情况下,我如何在C++中组织命令序列,就像我在图形命令中的例子一样?

c++ enums enumeration

2
推荐指数
1
解决办法
193
查看次数

根据某个字符串更改程序中的一个单词

所以我正在编写一个程序,我想要一些用户输入.假设我们定义一个int; 叫做年龄.如果用户是成年人(比方说30岁以上),我希望程序将所有非正式的"你"交换为"正式"你的(许多语言,如法语,有这种区别,想到vous vs tu)在荷兰语中,"u"是正式的而不是"je".

这样做最简洁的方法是什么?我现在有这个(使用今天的日期,2015年9月18日):

string abc;

if (2015 - year of birth > 30) {
    abc = "u";
}
else {
    abc = "je";
}
if (2015 - year of birth  == 30) {
    if ( September - month of birth  > 0) {
        abc = "u";
    }
    else {
        abc = "je";
    }
 }
 if (2015 - year of birth == 30) {
     if (September - month of birth == 0) {
         if (18 - day of birth …
Run Code Online (Sandbox Code Playgroud)

c++ date

2
推荐指数
1
解决办法
82
查看次数

在普通 for 循环中更改 QVector 的对象

如果我有一个 QVector,我可以使用基于范围的循环,使用引用并更改 QVector 中的对象。

但如果我在修改对象时需要索引,我必须使用普通的 for 循环。但是我怎样才能改变 QVector 中对象的值呢?

作为解决方法,我在更改临时对象后使用了替换方法,但这有点难看。

这是代码:

struct Resource {
    int value = 0;
};

int main(int argc, char *argv[])
{
    QVector<Resource> vector{Resource{}, Resource{}, Resource{}};

    qDebug() << vector.at(0).value
             << vector.at(1).value
             << vector.at(2).value;

    for(Resource &res : vector)
        res.value = 1;

    qDebug() << vector.at(0).value
             << vector.at(1).value
             << vector.at(2).value;

    for(int i = 0; i < vector.size(); ++i) {
        //Resource &res = vector.at(i); <-- won't compile: cannot convert from 'const Resource' to 'Resource &'
        Resource &res = vector.value(i); //Compiles, but …
Run Code Online (Sandbox Code Playgroud)

c++ qt containers qvector

2
推荐指数
1
解决办法
5330
查看次数

用同义词替换句子中每个单词的 Python 程序

基本上我想做的是创建一个程序,它接受一个句子/段落作为用户输入,查找每个单词的同义词,并用同义词替换该单词。到目前为止,我创建的程序运行完美,但存在一些问题/人为错误/逻辑错误。这是我现在所拥有的:

response=input("Enter what you want to thesaurize")
orig=response #puts user input into a string
num=orig.count(" ")+1 #finds number of words in the sentence
orig=orig.split(" ") #turns the sentence into a list of its words
new=[] #creates a new list to put the new words in, in case I'd want to go back to the original sentence for any reason
for i in range (num):
        if orig[i] not in badWords: #makes sure that the word is not a no-synonym word like …
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
3309
查看次数

Qt5 抛出 std::bad_alloc

我正在尝试在我的控制台应用程序中使用 QCustomPlot。我首先为它创建了一个适合我用途的简单包装器。包装器应该是但是,每次我尝试显示窗口时,我都会收到 std::bad_alloc 错误。

这是我的代码,我在以下位置创建了一个包装类Plot.hpp

class Plot
{
    private:
        std::string name;
        QApplication* app;
        QMainWindow* window;
        QCustomPlot* plotWidget;
    public:
        Plot(std::string& name);
        // OTHER METHODS
        void showPlot();
};
Run Code Online (Sandbox Code Playgroud)

在我的Plot.cpp文件中,我有以下内容:

Plot::Plot(std::string& name) : name(name)
{
    char *gui_argv[] = {(char*)(name.c_str()), NULL};
    int gui_argc = sizeof(gui_argv) / sizeof(char*) - 1;
    app = new QApplication(gui_argc, gui_argv);
    window = new QMainWindow();
    // Add plot Widget
    plotWidget = new QCustomPlot(window);
    window->setCentralWidget(plotWidget);
    plotWidget->plotLayout()->clear();
}

// OTHER METHODS

void Plot::showPlot()
{
    // Run the GUI
    window->show(); …
Run Code Online (Sandbox Code Playgroud)

c++ qt c++11 qt5 qcustomplot

2
推荐指数
1
解决办法
1060
查看次数

关于C语言中有符号和无符号整数的说明

我试过用C语言编写程序,程序结果如下:

输出:当iis 为0 unsigned int时,最后打印0 ,当值为i0时,控件不应该进入while循环,因为我已经给出了i>0条件,但是打印了0,我不明白它是怎么可能的.

i进行签名int,然后0不打印.

请让我知道该计划中发生了什么.

#include <stdio.h>
#include <string.h>

main()
{
   unsigned int i=1;
   int c=1;
   while(i>0)
   {
     i=i*2;         
     c++;
     printf("%d\n",i);          
   }

   printf("%d c value is",c) ;
}
Run Code Online (Sandbox Code Playgroud)

c

1
推荐指数
1
解决办法
114
查看次数

数组结构,结构数组和内存使用模式

我一直在阅读有关SOA的内容,我想在我正在构建的系统中尝试实现它.

我正在写一些简单的C结构来做一些测试,但我有点困惑,现在我有3个不同的结构vec3.我将在下面显示它们,然后进一步详细说明这个问题.

struct vec3
{
size_t x, y, z;
};

struct vec3_a
{
size_t pos[3];
};

struct vec3_b
{
size_t* x;
size_t* y;
size_t* z;
};

struct vec3 vec3(size_t x, size_t y, size_t z)
{
    struct vec3 v;
    v.x = x;
    v.y = y;
    v.z = z;
    return v;
}

struct vec3_a vec3_a(size_t x, size_t y, size_t z)
{
    struct vec3_a v;
    v.pos[0] = x;
    v.pos[1] = y;
    v.pos[2] = z;
    return v;
}

struct vec3_b vec3_b(size_t x, …
Run Code Online (Sandbox Code Playgroud)

c arrays struct cpu-cache

1
推荐指数
1
解决办法
190
查看次数

如何在Qt中将std :: string转换为QString?

我试图将字符串隐藏到QString,以便可以在QLineEdit或QLabel上显示该QString。这是我到目前为止拥有的一部分代码

char buff[100];
fgets(buff, sizeof(buff), fp);

//the buff[0] and buff[1] are two char values that I add up to make a string
std::string latitude = std::string() +  buff[0]  +  buff[1]; 
QString::fromStdString(latitude);

this->ui->lineEdit->setText(latitude);
Run Code Online (Sandbox Code Playgroud)

c++ qt string-conversion

1
推荐指数
1
解决办法
4144
查看次数

在 C 中写入文件时换行?

我目前正在 Linux 上编写一个程序,以从 /proc/stat 获取当前的 CPU 使用率并打印到 .txt 文件中。但是,在写入文件时,我无法打印新行,并且输出打印超过旧行...

我想在前一行下打印新行,但使用"\n""\r"字符不起作用。

代码在这里:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void checker();

int main(){

    long double a[4], b[4], loadavg;
    FILE *fp;
    char dump[50];

    for(;;){
        fp = fopen("/proc/stat","r");
        checker();
        fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&a[0],&a[1],&a[2],&a[3]);
        fclose(fp);
        sleep(1);

        fp = fopen("/proc/stat","r");
        checker();
        fscanf(fp,"%*s %Lf %Lf %Lf %Lf",&b[0],&b[1],&b[2],&b[3]);
        fclose(fp);

        fp = fopen("CPU_log.txt", "w");
        checker();
        loadavg = ((b[0]+b[1]+b[2]) - (a[0]+a[1]+a[2])) / ((b[0]+b[1]+b[2]+b[3]) - (a[0]+a[1]+a[2]+a[3]));
        fprintf(fp, "Current CPU Usage is: %Lf\r\n", loadavg);
        fclose(fp);
    }
    return …
Run Code Online (Sandbox Code Playgroud)

c fopen

1
推荐指数
1
解决办法
6595
查看次数