Codenameone 如何修复iOS摄像头的旋转图像(代号1)?
我已经写了一个代码来打开gallery并选择CodenameOne中的图像文件。在iOS上,它还允许捕获图像。当我在iOS上拍摄照片时,照片会旋转90°。当我从图库中拾取照片时,它不会旋转。如何固定90°旋转?以下是我打开画廊的代码:Codenameone 如何修复iOS摄像头的旋转图像(代号1)?,codenameone,Codenameone,我已经写了一个代码来打开gallery并选择CodenameOne中的图像文件。在iOS上,它还允许捕获图像。当我在iOS上拍摄照片时,照片会旋转90°。当我从图库中拾取照片时,它不会旋转。如何固定90°旋转?以下是我打开画廊的代码: Display.getInstance().openGallery((evt) -> { if (evt != null && evt.getSource() != null) { String fil
Display.getInstance().openGallery((evt) -> {
if (evt != null && evt.getSource() != null) {
String filePath = (String) evt.getSource();
Image img = null;
try {
img = Image.createImage(filePath);
} catch (IOException e) {
}
if (img != null) {
img = img.scaledLargerRatio(IMAGE_WIDTH_FOR_UPLOAD, IMAGE_HEIGHT_FOR_UPLOAD);
ImageHelper.saveToFileSystem("image" + ".jpg", img, null, null);
base64Img = (String) ImageHelper.loadBase64FromFileSystem("image" + ".jpg");
browser.execute("document.getElementById(\"" + QR_PHOTO_UPLOAD_IMG_ID + "\").setAttribute('src', '" + BASE64_PREFIX + base64Img + "');"
+ " document.getElementById(\"" + QR_PHOTO_UPLOAD_INPUT_ID + "\").setAttribute('base64', '" + BASE64_PREFIX + base64Img + "');"
+ "");
}
}
photoGalleryOpened = false;
}, Display.GALLERY_IMAGE);
以下是图像保存到文件系统的代码:
public static boolean saveToFileSystem(String path, Image capturedImage, String format, Float quality) {
if (format == null) {
format = ImageIO.FORMAT_JPEG;
}
if (quality == null) {
quality = 1f;
}
OutputStream os = null;
try {
String pathToBeStored = path.startsWith(APPHOMEPATH)? path : (APPHOMEPATH + path);
os = FileSystemStorage.getInstance().openOutputStream(pathToBeStored);
ImageIO.getImageIO().save(capturedImage, os, format, quality);
os.close();
} catch (Exception e) {
Log.e(e);
try {
if (os != null) {
os.close();
}
} catch (IOException ex) {
Log.e(ex);
}
return false;
} finally {
try {
if (os != null) {
os.close();
}
} catch (IOException ex) {
Log.p(ex.getMessage());
}
}
return true;
}
以下是从图像文件获取base64字符串的代码:
public static Object loadBase64FromFileSystem(String path) {
String ret = "";
try {
Image image = loadFromFileSystem(path);
if (image == null) {
throw new IOException("File not found (" + APPHOMEPATH + path + ")");
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.getImageIO().save(image, baos, ImageIO.FORMAT_JPEG, 1);
byte[] bytes = baos.toByteArray();
ret = Base64.encodeNoNewline(bytes);
} catch (Exception ex) {
Log.e(ex);
}
return ret;
}
public static Image loadFromFileSystem(String path) {
try {
File f = new File(APPHOMEPATH + path);
if(f.exists()){
Log.p("Image exists: " + f.exists());
Image img = Image.createImage(f.getPath());
return img;
}else{
Log.p("Image doesn't exist");
return null;
}
} catch (Exception ex) {
Log.e(ex);
}
Log.p("Image doesn't exist");
return null;
}
移动操作系统通常只拍摄一个方向的照片,然后在标签中将其标记为旋转。例如,照片始终是横向的,但当您拍摄纵向模式的照片时,会将其标记为旋转90度 大多数应用程序读取标签,并在加载后隐式旋转照片,这样做效果很好。有些应用程序不这样做,而本机图像读取代码在移动操作系统上也不这样做。我们需要显式地解析标记并进行旋转。我们这样做是为了捕获逻辑,但我们一般不这样做的图像加载
问题是,旋转图像非常昂贵,而且每次加载图像都会很昂贵(但这可能是我们在这种情况下必须做的事情)。现在我唯一能想到的解决办法是手动解析图像标签,这会有点痛苦。读者更新。现在,Codename One有了一个新的API来解决这个问题: