java - 处理3.0科泰相机。图片无法保存
问题描述
您好,我正在使用带有ketai 库的处理 3.0 ,我正在尝试保存图像,但由于某种原因它不起作用。每个按钮都有自己的侦听器,以识别它是否被按下。相机正常打开,但按下保存按钮时没有任何反应。处理控制台中会显示一条错误消息。控制台中显示的消息是:
未能创建目录来保存照片:/storage/emulated/0/Pictures/ testing6
testing6是我正在处理的 .pde 文件。另外,我正在安卓模拟器上测试应用程序,而不是在安卓设备上。我希望能够保存图像并创建一个包含图像的文件夹。例如,一个文件夹将有动物照片,另一个文件夹将有风景等。创建任意数量的文件夹和照片。我在 Ketai 库和 GitHub 上看到了文档,但找不到解决方案。
import ketai.camera.*;
import java.lang.String.*;
KetaiCamera camera;
void setup()
{
camera = new KetaiCamera(this,width,height/2,15);
// 0: back camera; 1: front camera
camera.setCameraID(0);
}
void draw()
{
image(camera, width/2, height/2, width, height);
drawUI();
}
void drawUI()
{
fill(255);
stroke(0);
orientation(LANDSCAPE);
//here there is a for loop to create the buttons when the camera open
//there are many buttons in other pages that is why we start from 28.
for(int i = 28; i <= 31; i++)
{
buttons[i].draw(color(0,128),textColor);
}
}
void onCameraPreviewEvent()
{
camera.read();
}
void onSavePhotoEvent(String filename)
{
camera.addToMediaLibrary(filename);
}
//mousePressed is a build-in function and I check which button was pressed.
//each button has on click listener.
void mousePressed()
{
if(buttons[28].isPressed()) //button Start, PAGE CAMERA
{
if (camera.isStarted())
{
camera.stop();
}
else
{
if (!camera.start())
{
println("Failed to start camera.");
}
}
}//end of if statement for the START button
else if(buttons[29].isPressed()) //button Save, PAGE CAMERA
{
if(camera.isStarted())
{
camera.savePhoto("test.png");
}
}//end of else if for the SAVE button
else if(buttons[30].isPressed()) //button Flash, PAGE CAMERA
{
if (camera.isFlashEnabled())
{
camera.disableFlash();
}
else
{
camera.enableFlash();
}
}//end of else if statement for the Flash button
else if(buttons[31].isPressed()) //button Exit, PAGE CAMERA
{
camera.stop();
}//end of else if statement for the Exit button
}//end of mousePressed function
解决方案
较新版本的 Android 要求用户在运行时而不是在安装期间访问敏感资源的权限。因此,在调用KetaiCamera::savePhoto()
.
void setup() {
requestPermission("android.permission.WRITE_EXTERNAL_STORAGE", "checkPermission");
}
void checkPermission(boolean wasPermissionGranted){
if (wasPermissionGranted)
println("Hooray! I can now write to the local file system!");
else
println("Oh no! I was not granted write permission =(");
}
推荐阅读
- python - Python Plotly:Surface autocolorscale 属性为 True,但抛出 Value Error '必须指定为 bool'
- php - 期望“Symfony\Component\Security\Core\User\UserInterface”的实例作为第一个参数
- python - 按两个第一个字符过滤值数组
- windows-7 - Gnuplot 5.2 不加载脚本文件
- c# - C# 表单应用程序尝试更改在运行时创建的组件值
- jenkins-pipeline - 如何在 Jenkins UI 中发布 Junit 报告文件夹
- node.js - 我可以使用 google 的 oauth 提供的访问令牌调用 google ad manager api 吗?
- typescript - 打字稿缩小不适用于嵌套函数的真实性
- java - 忽略或解决错误“类型必须实现继承的抽象方法”
- laravel - 如何使用 Veutify 数据表创建嵌套表结构