Home > Backend Development > PHP Tutorial > 如何用PHP代码实现这个Java代码所实现的。

如何用PHP代码实现这个Java代码所实现的。

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:25:32
Original
1231 people have browsed it

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

在尝试使用PHP写验证码识别,参考一个Java的代码。但是遇到这个不知道应该怎么用PHP写。

大概意思是打开目录中的图像,然后讲图像当做map的键,文件名做值。

然后我写了如下的代码,但是后面我不知道怎么办了。

<code>    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;
    }</code>
Copy after login
Copy after login

回复内容:

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

在尝试使用PHP写验证码识别,参考一个Java的代码。但是遇到这个不知道应该怎么用PHP写。

大概意思是打开目录中的图像,然后讲图像当做map的键,文件名做值。

然后我写了如下的代码,但是后面我不知道怎么办了。

<code>    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;
    }</code>
Copy after login
Copy after login

你的 train 目录下的文件名大概都是这样的吧? A B C D ... 0 1 2 3
Java 的那段代码是遍历目录, 然后建一个Map.

你PHP的代码应该类似于这样:

<code>$files[$filename] = file_get_contents($path.$filename);</code>
Copy after login

已解决。

<code>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;
    }
</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template