Skip to content

[feature] ocr QR screen #196

@dnsBlah

Description

@dnsBlah

Pull playername from screenshot, making use of ocr.space api

File based image

$ocrSpaceAPI = 'myAPIcodehere';
$uploadFile = 'local path to file to be uploaded';

$ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, 'https://api.ocr.space/parse/image');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'apikey: ' . $ocrSpaceAPI
    ));

    $imgType = pathinfo($uploadFile, PATHINFO_EXTENSION);
    $imgData = file_get_contents($uploadFile);
    $base64data = 'data:image/' . strtolower($imgType) . ';base64,' . base64_encode($imgData);
    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'base64Image' => trim($base64data)
    ));
    // Execute the request
    $chResponse = curl_exec($ch);
    if($chResponse === false) {
        //Something went wrong
    } else {
        $apiResult = json_decode($chResponse,true);
        if(is_array($apiResult)) {
            if(array_key_exists('OCRExitCode',$apiResult)) {
                if(intval($apiResult['OCRExitCode']) === 1) {
                    $ocrResults = '';
                    foreach($apiResult['ParsedResults'] as $partResult) {
                        $ocrResults .= $partResult['ParsedText'];
                    }                    
                    $ocrLines = explode('
',$ocrResults);
                    $tTrainername = false;
                    foreach($ocrLines as $ocrLine) {
                        if($tTrainername === true) {
                            $tTrainername = trim($ocrLine);
                            break;
                        }
                        if(stristr($ocrLine, 'TRAINER NAME')) {
                            $tTrainername = true;
                        }
                    }
                    if($tTrainername !== false && $tTrainername !== true) {
////Trainer name found here////
                    } else {
                        echo 'Unable to parse trainername?!';
                    }
                } else {
                    //Something went wrong
                    /*
                     * $apiResult['ErrorMessage']
                     * $apiResult['ErrorDetails']
                     */
                }
            } else {
                //Something went wrong, unexpected response
            }
        } else {
            //Something went wrong response from api is not an json array
        }
    }

URL based image

$ocrSpaceAPI = 'myAPIcodehere';
$imageURL = 'full public path to image';

$ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, 'https://api.ocr.space/parse/image');
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'apikey: ' . $ocrSpaceAPI
    ));

    curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'url' => trim($imageURL)
    ));
    // Execute the request
    $chResponse = curl_exec($ch);
    if($chResponse === false) {
        //Something went wrong
    } else {
        $apiResult = json_decode($chResponse,true);
        if(is_array($apiResult)) {
            if(array_key_exists('OCRExitCode',$apiResult)) {
                if(intval($apiResult['OCRExitCode']) === 1) {
                    $ocrResults = '';
                    foreach($apiResult['ParsedResults'] as $partResult) {
                        $ocrResults .= $partResult['ParsedText'];
                    }                    
                    $ocrLines = explode('
',$ocrResults);
                    $tTrainername = false;
                    foreach($ocrLines as $ocrLine) {
                        if($tTrainername === true) {
                            $tTrainername = trim($ocrLine);
                            break;
                        }
                        if(stristr($ocrLine, 'TRAINER NAME')) {
                            $tTrainername = true;
                        }
                    }
                    if($tTrainername !== false && $tTrainername !== true) {
////Trainer name found here////
                    } else {
                        echo 'Unable to parse trainername?!';
                    }
                } else {
                    //Something went wrong
                    /*
                     * $apiResult['ErrorMessage']
                     * $apiResult['ErrorDetails']
                     */
                }
            } else {
                //Something went wrong, unexpected response
            }
        } else {
            //Something went wrong response from api is not an json array
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesthelp wantedWorth doing but needs an owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions