#include "pictobase64model.h"
#include <QBuffer>
#include <QDebug>
#include <QImage>
#include <QTime>
#include <QMutex>
static QMutex m_PicToBase64Mutex;
static QMutex m_LeftPicToBase64Mutex;
static QMutex m_CarPicToBase64Mutex;
static QMutex m_CarPlnumPicToBase64Mutex;
PicToBase64Model::PicToBase64Model(QObject *parent) : QObject(parent)
{

}
void PicToBase64Model::RecvFromBridge_picPath(QString datapath)
{
    m_PicToBase64Mutex.lock();
    if(datapath.isEmpty()){
        m_PicToBase64Mutex.unlock();
        return;
    }
    QTime timer;
    timer.start();
    QByteArray data = Image_To_Base64(datapath);
    m_PicToBase64Mutex.unlock();
    qDebug()<< "图片转换用时："<<timer.elapsed()<<"ms";
    emit SendToBridge_Base64data(data);

}
QByteArray PicToBase64Model::Image_To_Base64(QString ImgPath)
{
    QImage image(ImgPath);
    QByteArray base;
    QBuffer buf(&base);
    image.save(&buf,"JPG",100);
    QByteArray hex = base.toBase64();
    buf.close();
    return hex;
}

PicToBase64Model_LeftPic::PicToBase64Model_LeftPic(QObject *parent)
{

}

void PicToBase64Model_LeftPic::RecvFromBridge_picPath(QString datapath)
{
    m_LeftPicToBase64Mutex.lock();
    if(datapath.isEmpty()){
        m_LeftPicToBase64Mutex.unlock();
        return;
    }
    QTime timer;
    timer.start();
    QByteArray data = Image_To_Base64(datapath);
    m_LeftPicToBase64Mutex.unlock();
    qDebug()<< "图片转换用时："<<timer.elapsed()<<"ms";
    emit SendToBridge_Base64data(data);
}

QByteArray PicToBase64Model_LeftPic::Image_To_Base64(QString ImgPath)
{
    QImage image(ImgPath);
    QByteArray base;
    QBuffer buf(&base);
    image.save(&buf,"JPG",100);
    QByteArray hex = base.toBase64();
    buf.close();
    return hex;
}

PicToBase64Model_CarPic::PicToBase64Model_CarPic(QObject *parent)
{

}

void PicToBase64Model_CarPic::RecvFromBridge_picPath(QString datapath)
{
    m_CarPicToBase64Mutex.lock();
    if(datapath.isEmpty()){
        m_CarPicToBase64Mutex.unlock();
        return;
    }
    QTime timer;
    timer.start();
    QByteArray data = Image_To_Base64(datapath);
    m_CarPicToBase64Mutex.unlock();
    qDebug()<< "图片转换用时："<<timer.elapsed()<<"ms";
    emit SendToBridge_Base64data(data);
}

QByteArray PicToBase64Model_CarPic::Image_To_Base64(QString ImgPath)
{
    QImage image(ImgPath);
    QByteArray base;
    QBuffer buf(&base);
    image.save(&buf,"JPG",100);
    QByteArray hex = base.toBase64();
    buf.close();
    return hex;
}

PicToBase64Model_CarPlnumPic::PicToBase64Model_CarPlnumPic(QObject *parent)
{

}

void PicToBase64Model_CarPlnumPic::RecvFromBridge_picPath(QString datapath)
{
    m_CarPlnumPicToBase64Mutex.lock();
    if(datapath.isEmpty()){
        m_CarPlnumPicToBase64Mutex.unlock();
        return;
    }
    QTime timer;
    timer.start();
    QByteArray data = Image_To_Base64(datapath);
    m_CarPlnumPicToBase64Mutex.unlock();
    qDebug()<< "图片转换用时："<<timer.elapsed()<<"ms";
    emit SendToBridge_Base64data(data);
}

QByteArray PicToBase64Model_CarPlnumPic::Image_To_Base64(QString ImgPath)
{
    QImage image(ImgPath);
    QByteArray base;
    QBuffer buf(&base);
    image.save(&buf,"JPG",100);
    QByteArray hex = base.toBase64();
    buf.close();
    return hex;
}
