【发布时间】:2016-05-27 04:46:56
【问题描述】:
好的,所以我正在编写一个应该需要 10 分钟才能完成的小程序,但是我遇到了无法预料的问题。
该程序应该接收我在旧手机上的保险库程序中的一些旧文件,它们基本上是 Jpg 文件,但在文件前面添加了“模糊”文本。
所以下面是我的代码逻辑
- 获取文件的文件夹输入,
- 创建一个包含每个实际文件的数组列表。
- 调用 ConvertFiles 将文件转换为字符串,
- 使用子字符串删除前 8 个字符并将该临时文件保存到另一个包含字符串的数组列表中。
- 将该字符串解码为 base64 并将其输入到 bytearrayinputstream 并将其保存到缓冲图像。
这就是问题发生的地方。我的内容一直到 ImageIO.read(bis),所以当它尝试写入新文件时,它会抛出 image == null 来自 ImageTypeSpecifier。我尝试了多种解码和编码字符串的方法,但需要任何帮助,如果需要更多信息,我会提供!
public class ImageConvert {
private File folder;
private ArrayList<File> files;
private ArrayList<String> stringFiles = new ArrayList<>();
private ArrayList<BufferedImage> bfImages = new ArrayList<>();
boolean isRunning = true;
Scanner scanner = new Scanner(System.in);
String folderPath;
public static void main(String[] args) {
ImageConvert mc = new ImageConvert();
mc.mainCode();
}
public void mainCode(){
System.out.println("Please enter the folder path: ");
folderPath = scanner.nextLine();
folder = new File(folderPath);
//System.out.println("folderpath: " + folder);
files = new ArrayList<File>(Arrays.asList(folder.listFiles()));
convertFiles();
}
public void convertFiles(){
for(int i = 0; i < files.size(); i++){
try {
String temp = FileUtils.readFileToString(files.get(i));
//System.out.println("String " + i + " : " + temp);
stringFiles.add(temp.substring(8));
} catch (IOException ex) {
Logger.getLogger(ImageConvert.class.getName()).log(Level.SEVERE,
null, ex);
}
}
//System.out.println("Converted string 1: " + stringFiles.get(0));
for(int j = 0; j < stringFiles.size(); j++){
BufferedImage image = null;
byte[] imageByte;
try {
BASE64Decoder decoder = new BASE64Decoder();
imageByte = decoder.decodeBuffer(stringFiles.get(j));
System.out.println(imageByte.toString());
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
bis.close();
bfImages.add(image);
} catch (IOException ex) {
Logger.getLogger(ImageConvert.class.getName()).log(Level.SEVERE,
null, ex);
}
}
System.out.println("Image 1: " + bfImages.get(0));
for(int k = 0; k < bfImages.size(); k++){
try {
ImageIO.write(bfImages.get(k), "jpg",
new File(folderPath + "/" + k + ".jpg"));
} catch (IOException ex) {
Logger.getLogger(ImageConvert.class.getName()).log(Level.SEVERE,
null, ex);
}
}
}
}
【问题讨论】:
-
您的问题因缺少段落而无法阅读。
-
大声笑......无论如何,我认为十六进制是 Base 16(不是 64)。看看你是如何设法让你的图像以 jpg 格式显示的会很有趣。
-
你的模糊文本包含多少个字符
-
它实际上是“模糊的”,所以 8 个字符,这就是为什么我的子字符串(8),而不是图像存储在 base64 中?我想我使用 base 16 进行编辑,但是我将如何将 base16 保存到 JPG 文件中?
标签: java string stream java-io