背景
含有错误信息的图片是检测不到人脸的。
- 错误的读文件方式,会过滤掉图片的exif信息。
- 验证:将该错误的base64值传入https://www.hitoy.org/tool/file_base64.php生成图片;并将图片放入http://metapicz.com/#landing中,发现并没有exif信息。
通过工具查看,其实图片的方向是旋转了270度的。
获取的图片的方向是向上的
运行结果,读出来的值的确是不太一样。
直接上干货,都是代码
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Base64;
public class photo {
public static void main(String [] args) {
// 错误的读文件方式,会过滤掉图片的exif信息。
// 验证:将该错误的base64值传入https://www.hitoy.org/tool/file_base64.php生成图片;并将图片放入http://metapicz.com/#landing中,发现并没有exif信息。
//File input = new File("C:UsersailanzhouPicturesSaved Pictures07181.jpg");
//File input = new File("C:UsersailanzhouPictures12051.jpg");
File input = new File("C:Usersp_yingfaluDesktop04Picture04_10_01.jpg");
try {
BufferedImage bufferedImage = ImageIO.read(input);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"jpg",os);
byte[] bytes = os.toByteArray();
String a = Base64.getEncoder().encodeToString(bytes);
System.out.println(a);
}catch ( Exception e ){
e.printStackTrace(System.err);
}
// 正确的读文件方式,不会过滤掉图片的exif信息。生成的base64与https://www.hitoy.org/tool/file_base64.php一致。
try {
//InputStream in = new FileInputStream("C:UsersailanzhouPicturesSaved Pictures07181.jpg");
InputStream in = new FileInputStream("C:Usersp_yingfaluDesktop04Picture04_10_01.jpg");
byte[] data = new byte[in.available()];
in.read(data);
in.close();
String b = Base64.getEncoder().encodeToString(data);
System.out.println(b);
}
catch ( Exception e ){
e.printStackTrace(System.err);
}
}
//
}
这是简书上实现的 https://www.jianshu.com/p/9513041db67c