Android 奇维普莱尔照相机
我试着用Plyer相机制作一个小应用程序Android 奇维普莱尔照相机,android,kivy,Android,Kivy,我试着用Plyer相机制作一个小应用程序 def take_shot(self, *args): self.filepath = IMAGE_PATH self.time = datetime.now().strftime("%Y%m%d_%H%M%S%f") self.filename = '{0}/IMG_{1}.jpg'.format(self.filepath, self.time) try: camera.take_picture(fil
def take_shot(self, *args):
self.filepath = IMAGE_PATH
self.time = datetime.now().strftime("%Y%m%d_%H%M%S%f")
self.filename = '{0}/IMG_{1}.jpg'.format(self.filepath, self.time)
try:
camera.take_picture(filename=self.filename, on_complete=self.complete_callback)
except NotImplementedError:
self.camera_status = 'Camera is not implemented for your platform'
def complete_callback(self):
try:
im = Image.open(self.filename)
im.thumbnail(Window.size)
outfile = '{0}/IMG_{1}-thumbnail.jpg'.format(self.filepath, self.time)
im.save(outfile, "JPEG")
except Exception as e:
self.error = str(e)
return False
但是:
我解决了一个问题。函数
complete\u callback
必须接受一个参数filename
,我修复了它,现在所有的函数都可以工作了
def take_shot(self, *args):
self.time = datetime.now().strftime("%Y%m%d_%H%M%S%f")
filename = '{0}/IMG_{1}.jpg'.format(IMAGE_PATH, self.time)
try:
camera.take_picture(filename=filename, on_complete=self.complete_callback)
except NotImplementedError:
self.camera_status = 'Camera is not implemented for your platform'
def complete_callback(self, filename):
try:
im = Image.open(filename)
im.thumbnail(Window.size)
outfile = '{0}/IMG_{1}.jpg'.format(THUMBNAIL_PATH, self.time)
im.save(outfile, "JPEG")
except Exception as e:
self.error = str(e)
return True
但是,所有kivy文件只有在设备重新启动后才会出现,我认为我的设备存在这个问题。我在Android 5.0.2上使用摩托罗拉Moto G。因此,我最终解决了gallery的问题,现在我的代码如下所示:
def take_shot(self, *args):
self.time = datetime.now().strftime("%Y%m%d_%H%M%S%f")
filename = '{0}/IMG_{1}.jpg'.format(IMAGE_PATH, self.time)
try:
camera.take_picture(filename=filename, on_complete=self.complete_callback)
except NotImplementedError:
self.camera_status = 'Camera is not implemented for your platform'
def complete_callback(self, filename):
try:
Intent = autoclass('android.content.Intent')
PythonActivity = autoclass('org.renpy.android.PythonActivity')
Uri = autoclass('android.net.Uri')
# Push photo into gallery
context = PythonActivity.mActivity
intent = Intent()
uri = 'file://{0}'.format(filename)
intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
intent.setData(Uri.parse(uri))
context.sendBroadcast(intent)
im = Image.open(filename)
im.thumbnail(Window.size)
outfile = '{0}/IMG_{1}.jpg'.format(THUMBNAIL_PATH, self.time)
im.save(outfile, "JPEG")
except Exception as e:
Logger.error(str(e))
self.error = str(e)
return False
我希望这对某人有所帮助。胡乱猜测:照片在右侧的图库中不可见,因为没有调用您的
complete\u回调
。如果应用程序返回一些奇怪的内容,则从应用程序发布日志,否则从logcat发布的日志就足够了。不,我发现complete\u callback
中的错误在哪里-它获取filename
参数,但照片在库中仍然不可见。所有kivy文件只有在设备重新启动后才会显示。我在Android 5.0.2上使用摩托罗拉Moto G。试试其他设备,可能在多个设备上都是这样。