Kivy Plyer相机

时间:2016-04-28 11:13:54

标签: android kivy

我尝试用Plyer相机制作小应用程序。

this.context = context;

可是:

  1. 当我拍摄时,照片在设备的图库中不可见,只有在设备重置后才会显示。
  2. 不调用函数complete_callback。

2 个答案:

答案 0 :(得分:3)

所以,我终于解决了库的问题,现在我的代码看起来像这样:

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

我希望这有助于某人。

答案 1 :(得分:1)

我解决了一个问题。函数complete_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 Moto G和Android 5.0.2。