0

我已经完成了一个代码来通过在我的 qt 应用程序中使用相机来捕获图像。我正在使用 QtAndroid 在 Qt 中编写等效的 java 代码。

我已经打开相机,拍摄了图像,但是 uri 返回 null 并且突然应用程序在上述 android pie 版本手机上崩溃,并且它在 android lolipop 手机上正常工作。

如何使用 QAndroid 获取捕获图像的 uri 以在 Qt 中编写 Java 等效代码?

这是我所做的代码:

打开相机的代码:-

QAndroidJniObject ACTION_IMAGE_CAPTURE = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore", "ACTION_IMAGE_CAPTURE", "Ljava/lang/String;");
QAndroidJniObject intent("android/content/Intent", "(Ljava/lang/String;)V", ACTION_IMAGE_CAPTURE.object<jstring>());
QtAndroid::startActivity(intent.object<jobject>(), 101, this);

获取图像 uri 的代码:-

void UiMan::handleActivityResult(int receiverRequestCode, int resultCode, const QAndroidJniObject 
&data)
{
  qDebug()<<"In handelActivityResult";
  jint RESULT_OK = QAndroidJniObject::getStaticField<jint>("android/app/Activity", "RESULT_OK");
  if (receiverRequestCode == 101 && resultCode == RESULT_OK)
  {
    qDebug()<<"In If";
    QAndroidJniObject uri = data.callObjectMethod("getData", "()Landroid/net/Uri;");
    QAndroidJniObject dadosAndroid = QAndroidJniObject::getStaticObjectField("android/provider/MediaStore$MediaColumns", "DATA", "Ljava/lang/String;");
    QAndroidJniEnvironment env;
    jobjectArray projecao = (jobjectArray)env->NewObjectArray(1, env->FindClass("java/lang/String"), NULL);
    jobject projacaoDadosAndroid = env->NewStringUTF(dadosAndroid.toString().toStdString().c_str());
    env->SetObjectArrayElement(projecao, 0, projacaoDadosAndroid);
    QAndroidJniObject contentResolver = QtAndroid::androidActivity().callObjectMethod("getContentResolver", "()Landroid/content/ContentResolver;");
    QAndroidJniObject cursor = contentResolver.callObjectMethod("query", "(Landroid/net/Uri;[Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)Landroid/database/Cursor;", uri.object<jobject>(), projecao, NULL, NULL, NULL);
    jint columnIndex = cursor.callMethod<jint>("getColumnIndex", "(Ljava/lang/String;)I", dadosAndroid.object<jstring>());
    cursor.callMethod<jboolean>("moveToFirst", "()Z");
    QAndroidJniObject resultado = cursor.callObjectMethod("getString", "(I)Ljava/lang/String;", columnIndex);

    QFile imageFile(resultado.toString());
    QDir dir;
    if(dir.exists("images")){
        qDebug()<<"Folder already exists";
    }else {
        dir.mkdir("images");
    }
    QStringList splitedImagePath = resultado.toString().split("/");
    QString imageName = splitedImagePath.value(splitedImagePath.length() - 1);
    QString dataimgFile = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/images/" + imageName;
    // /data/data/org.qtproject.feather/files/images/offLight_Button.png

    if (imageFile.exists())
    {
        QFile cdbfile(dataimgFile);
        if (!cdbfile.exists())
        {
            if(!imageFile.copy(dataimgFile))
                qDebug() << "Error :copy failed" ;

            QFile::setPermissions(dataimgFile, QFile::WriteOwner | QFile::ReadOwner);
        }

        else
        {
            qDebug() << "image exits allready no need to copy " ;
            QFile::setPermissions(dataimgFile, QFile::WriteOwner | QFile::ReadOwner);
        }

    }
    else
    {
        qDebug() << "img Not exit in Assert " << dataimgFile;

    }

    QVariant imagePath = "file://" + dataimgFile;

    emit signalToQml(1148, imagePath, "", "","","","","","");
}
else
{
    qDebug() << "Error";
}

}

所以。请帮我解决这个问题。并帮助我编写等效的 qt 代码,以获取捕获图像的位图,

Bitmap bitmap = (Bitmap) data.getExtras().get("data");
4

0 回答 0