简体   繁体   English

无法找到图像文件Java

[英]Image file cannot be located java

I'm using eclipse, my code: - 我正在使用eclipse,我的代码:-

package arrowRecog;

import org.sikuli.api.*;
import java.io.File;
import arrowRecog.res.*;

public class FocusTrain
{
    public static void main(String[] args) throws Exception
    {
         Target oneDown = new ImageTarget(new File("arrowRecog.res","1down.jpg"));
    }
}

The Tree: 那个树:

http://i1316.photobucket.com/albums/t601/Preformer/tree_zps1e15285b.jpg http://i1316.photobucket.com/albums/t601/Preformer/tree_zps1e15285b.jpg

The Exception: 例外:

http://i1316.photobucket.com/albums/t601/Preformer/error_zps05bc7502.jpg http://i1316.photobucket.com/albums/t601/Preformer/error_zps05bc7502.jpg

The problem is the "\\1" part of the string literal. 问题是字符串文字的“ \\ 1”部分。 That's not a backslash followed by a 1 - that's an octal escape sequence, yielding U+0001. 这不是反斜杠,后跟1-这是八进制转义序列,产生U + 0001。 To get the string you were aiming for, you want: 要获取您想要的字符串,您需要:

Target oneDown = new ImageTarget(new File("arrowRecog.res\\1down.jpg"));

Or better - more portable: 或更好-更便携:

Target oneDown = new ImageTarget(new File("arrowRecog.res/1down.jpg"));

Or even better: 甚至更好:

Target oneDown = new ImageTarget(new File("arrowRecog.res", "1down.jpg"));

(In practice, every platform I've used Java on has coped with / as a directory separator, but using the File constructor taking two strings is still a good idea in general.) (实际上,我使用过Java的每个平台都可以将/用作目录分隔符,但是使用File构造函数使用两个字符串通常仍然是一个好主意。)

I have no idea why you put non-sourcecode resources into src directory. 我不知道为什么要将非源代码资源放入src目录。 Directories here are interpreted by Eclipse as packages, so they are shown in the "folder.subfolder" format. Eclipse将这里的目录解释为软件包,因此它们以“ folder.subfolder”格式显示。 The File class is using the filesystem, not java class path format, so "arrowRecog.res" will not be interpreted as "arrowRecog" directory with subdirectory "res", but it will be searching for "arrowRecog.res" directory - which does not exist. File类使用的是文件系统,而不是Java类路径格式,因此“ arrowRecog.res”不会被解释为带有“ res”子目录的“ arrowRecog”目录,但是它将搜索“ arrowRecog.res”目录-不存在。 In other words, the dot will not be taken for path delimiter, but as part of the directory name. 换句话说,点将不被用作路径定界符,而是作为目录名的一部分。

Also, since you are using relative paths, that means relative to the working directory. 另外,由于您使用的是相对路径,所以这意味着相对于工作目录。 The working directory is by default the main directory of the project in Eclipse (if I remember correctly). 默认情况下,工作目录是Eclipse中项目的主目录(如果我没记错的话)。 So you are missing the "src" dir in your paths. 因此,您在路径中缺少“ src”目录。

new File("src/arrowRecog/res/1down.jpg")

or 要么

new File("arrowRecog/res/1down.jpg")

should probably work, depending on how your working directory is set. 应该可以工作,具体取决于工作目录的设置方式。

The best thing to do is create a new directory next to the src directory (called res for example) and all images move there. 最好的办法是在src目录旁边创建一个新目录(例如,称为res ),所有图像都移至该目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM