How to implement this Java code in PHP code.

Source: Internet
Author: User
public static Map
  
   
    
    loadTrainData() throws Exception {        Map
   
    
     
     map = new HashMap
    
     
      
     ();        File dir = new File("train");        File[] files = dir.listFiles();        for (File file : files) {            map.put(ImageIO.read(file), file.getName().charAt(0) + "");        }        return map;    }
    
     
   
    
  
   

In the attempt to use PHP to write captcha identification, refer to a Java code. But I don't know how to write in PHP when I encounter this.

The idea is to open the image in the directory, and then say the image as the map key, the file name to do the value.

Then I wrote the following code, but I didn't know what to do later.

    public function loadTrainData()    {        $data = array();        $path = "train/train1/";        $handler = opendir($path);          while (($filename = readdir($handler)) !== false) {              if ($filename != "." && $filename != "..") {                  $files[] = file_get_contents($path.$filename);           }          }        closedir($handler);         #code 这里应该怎么写,还是说有更好的写法去写这个函数                return $data;    }

Reply content:

public static Map
  
   
    
    loadTrainData() throws Exception {        Map
   
    
     
     map = new HashMap
    
     
      
     ();        File dir = new File("train");        File[] files = dir.listFiles();        for (File file : files) {            map.put(ImageIO.read(file), file.getName().charAt(0) + "");        }        return map;    }
    
     
   
    
  
   

In the attempt to use PHP to write captcha identification, refer to a Java code. But I don't know how to write in PHP when I encounter this.

The idea is to open the image in the directory, and then say the image as the map key, the file name to do the value.

Then I wrote the following code, but I didn't know what to do later.

    public function loadTrainData()    {        $data = array();        $path = "train/train1/";        $handler = opendir($path);          while (($filename = readdir($handler)) !== false) {              if ($filename != "." && $filename != "..") {                  $files[] = file_get_contents($path.$filename);           }          }        closedir($handler);         #code 这里应该怎么写,还是说有更好的写法去写这个函数                return $data;    }

is the train file name in your directory probably the same?A B C D ... 0 1 2 3
JavaThe code is to traverse the directory and then build one Map .

Your PHP code should look similar to this:

$files[$filename] = file_get_contents($path.$filename);

has been resolved.

public function loadTrainData()    {        $path = "train/tran1/";        $handler = opendir($path);          while (($filename = readdir($handler)) !== false) {              if ($filename != "." && $filename != "..") {                  $sampleMap[substr($filename, 0, 1)] = imagecreatefromjpeg($path.$filename);           }          }        closedir($handler);         return $sampleMap;    }
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.