ZXing.NetでQRコード読み取り(C#)

仕事で必要になったのでメモ。
ZXingのバグ?か、BarcodeReader.Decodeメソッドに食わせる画像によっては、
例外発生するみたい。
(公式のサンプルプログラムでも同様の現象を確認)

private BarcodeReader barcodeReader = new BarcodeReader();    //クラスのメンバとする

public Form1() {
    InitializeComponent();

    barcodeReader.AutoRotate = true;
    barcodeReader.TryInverted = true;
            
}

private string readQR(Bitmap bmp) {
    string ret = "(not found)";

    try {
        Result result = barcodeReader.Decode(bmp);
        if (result != null) {
            ret = result.Text;
        }
        }
        catch {
            //ZXingのバグ?barcodeReader.Decodeに失敗することがある ver 0.14.0.0 で確認
        }

        return ret;
    }
}

Qt(MinGW)+Freeglutでティーポットくるくるできたよ~

ようやくできた。忘れないうちにメモメモ。

環境はWin8.1+Qt5.3(MinGW)+freeglut-MinGW-2.8.1。

 

freeglutは解凍したファイルを以下のようにコピー。

・includeフォルダ内のGLフォルダ→Qt\Tools\mingw482_32\i686-w64-mingw32\includeへ

・libフォルダの中身→Qt\Tools\mingw482_32\i686-w64-mingw32\libへ

 

 で、コードは以下の通り。

.proファイル

QT       += core gui opengl

TARGET = OpenGLTest
TEMPLATE = app

SOURCES += main.cpp\
        mainwindow.cpp \
    glwidget.cpp

HEADERS  += mainwindow.h \
    glwidget.h

FORMS    += mainwindow.ui

LIBS += -lfreeglut


main.cpp

#include "mainwindow.h"
#include 
#include <GL/glut.h>

int main(int argc, char *argv[])
{
    glutInit(&argc,argv);
    QApplication a(argc, argv);

    MainWindow w;
    w.show();

    return a.exec();
}


MainWindow.h

    #ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H


MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


glwidget.h

#ifndef GLWIDGET_H
#define GLWIDGET_H

#include 
#include 
#include <GL/glut.h>

class GLWidget : public QGLWidget
{
    Q_OBJECT
public:
    explicit GLWidget(QWidget *parent = 0);

    void initializeGL();
    void paintGL();
    void resizeGL(int w, int h);

    void mousePressEvent(QMouseEvent *e);

private:
    QTimer timer;

};

#endif // GLWIDGET_H

glwidget.cpp

#include "glwidget.h"
#include <GL/glut.h>
#include <GL/glu.h>
#include 

GLWidget::GLWidget(QWidget *parent) :
    QGLWidget(parent)
{
    connect(&timer,SIGNAL(timeout()),this,SLOT(updateGL()));
    timer.start(16);
}

void GLWidget::initializeGL(){
    glClearColor(0.2,0.2,0.2,1);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glEnable(GL_COLOR_MATERIAL);

}


void GLWidget::paintGL(){
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glRotatef(0.5, 1,1,1);
    glColor3f(1,0.6,0);
    glutSolidTeapot(0.6);
//    glBegin(GL_TRIANGLES);
//        glColor3f(1,0,0);
//        glVertex3f(-0.5, -0.5, 0);
//        glColor3f(0,1,0);
//        glVertex3f( 0.5, -0.5, 0);
//        glColor3f(0,0,1);
//        glVertex3f( 0.0,  0.5, 0);
//    glEnd();
}

void GLWidget::resizeGL(int w, int h){
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45,(float)w/h,0.01,100.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0,0,5, 0,0,0, 0,1,0);
}

void GLWidget::mousePressEvent(QMouseEvent *e){
    //if mouse clicked,timer will stop

    if(e->button() == Qt::LeftButton){
        if(timer.isActive()){
            timer.stop();
        }
        else{
            timer.start();
        }
    }
}

んで、実行結果はこんな感じ。

f:id:ossannt:20140711190854p:plain

 

Ubuntuとかだったら楽勝だったのに、Win上では1週間くらいかかった・・・。

わかってしまえばどうってことないのよねorz


(C#)BackgroundImageが描画速度に与える影響

FormにBackGroundImageを設定すると、なんでか知らんがForm上の他のコントロールの描画速度が劇的に遅くなるみたい。

PictureBoxを20個程度並べたFormを作ってたんだけど、FormにBackGroundImageを設定した状態だと、Pictureboxが1個ずつ表示されてた。

けど、BackgroundImageを外したら一瞬で表示されるように。

なんでこんなことになるんだろうな・・・

(C#)PictureBoxを透過させる

まーたドツボにはまったのでメモ。

 

複数のPictureBoxを重ねて、かつ透過させて使いたい場合、

BackColorプロパティをColor.Transparentにするのは当然。

このとき、透けて見えるのはそのPictureBoxの親だということを失念してました。

 

なので、フォームのコンストラクタやLoadイベントなどで、

childPictureBox.Parent = parentPictureBox;

などと設定してやると幸せになれるかも。

(C# & C++/CLI) C++のスタティックライブラリをC#から使う

επιστημηさんの記事

Vista到来。既存C/C++資産の.NET化を始めよう! − @IT

と同じことをVS2012でやろうとしたらちょっとハマったのでメモ。

 

C++/CLIのラッパーライブラリのリンカ設定で、

追加の依存ファイルを設定しないと、リンクエラーになる。

具体的には、C++(ネイティブ)の.libを明示的に指定しないとダメみたい。