One, foreword

For the transmission of the picture, a common practice, generally there are two, one is direct binary file transfer, such as transmission first start tag, with the file name, file bytes, then each subcontract to send, the last to send end of file marker, this way must request the receiving party shall, in accordance with the definition of the rules for themselves to receive the data file, so the generality is limited, Either request for uploading files in the form of HTTP and process the files according to the standard file uploading mechanism, but this limits application scenarios. For example, in most cases, the transmission needs to be directly performed using the underlying TCP or UDP protocol, or the display is displayed after the transmission is directly completed.

This is derived from the second common practice, conversion into Base64 encoding for transmission, the existing various language platform development framework, basically there will be base64 encoding conversion functions or classes, there is no doubt that Qt also have, as a super large GUI development supermarket, this is certainly a necessary basic function, The only disadvantage of switching to Base64 encoding is that the data is much larger.

There is also a common scenario that will use base64 encoding is Chinese transmission, such as using TCP protocol, if both sides are UTF8 encoding it is easy to say, the key is that many may be GBK encoding, you may not know in the past, common serial transceiver Chinese, network data transceiver Chinese, is a headache, Compatible to the best way is to direct the Chinese into base64 encoding, the other party after receiving the base64 decoding, easily done, I have been a company related projects, there are a lot of third-party platform of communication, or the interface specification of provided to a third party, the Chinese are in accordance with the stipulations of the base64 encoding to never had problems.

Base64 coding:

  1. Base64 is one of the most common encoding methods for transmitting 8Bit bytecode on the network.
  2. Base64 is a way to represent binary data based on 64 printable characters.
  3. Base64 encoding is a process from binary to character that can be used to pass longer identity information in HTTP environments.
  4. Base64 encoding is unreadable and can be read only after decoding.
  5. Base64 encoding can be used to pass longer identity information in HTTP environments.

Two, the main functions

  1. Picture to base64 string.
  2. Base64 character string to image.
  3. Character to base64 character string.
  4. Base64 The character string turns to a character.
  5. Add data compression later.
  6. Qt6 rewrites base64 encoding conversions to improve efficiency by at least 200%.

Three, effect drawing

Open source homepage

Above works complete source download are on the open source home page, will continue to update the number and quality of works, welcome to pay attention to. This open source project has been successfully upgraded to V2.0 version, categorized, illustrated, protect you cool to blast.

  1. Domestic site: gitee.com/feiyangqing…
  2. International site: github.com/feiyangqing…
  3. Personal website: qtchina.blog.csdn.net/
  4. Zhihu homepage: www.zhihu.com/people/feiy…

Five, core code

#include "base64helper.h"
#include "qbuffer.h"
#include "qdebug.h"

QString Base64Helper::imageToBase64(const QImage &image)
{
    return QString(imageToBase64x(image));
}

QByteArray Base64Helper::imageToBase64x(const QImage &image)
{
    // This conversion may be time-consuming and is recommended to be performed in a thread
    QByteArray data;
    QBuffer buffer(&data);
    image.save(&buffer, "JPG");
    data = data.toBase64(a);return data;
}

QImage Base64Helper::base64ToImage(const QString &data)
{
    return base64ToImagex(data.toUtf8());
}

QImage Base64Helper::base64ToImagex(const QByteArray &data)
{
    // This conversion may be time-consuming and is recommended to be performed in a thread
    QImage image;
    image.loadFromData(QByteArray::fromBase64(data));
    return image;
}

QString Base64Helper::textToBase64(const QString &text)
{
    return QString(text.toLocal8Bit().toBase64());
}

QString Base64Helper::base64ToText(const QString &text)
{
    return QString(QByteArray::fromBase64(text.toLocal8Bit()));
}
Copy the code