2016-04-28 125 views
1

我尝试使用Plyer相机制作一个小应用程序。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(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 

但是:

  1. 当我进行拍摄,照片是不是在设备上的画廊可见,看来只有在设备复位。
  2. 函数complete_callback不被调用。
+0

疯狂的猜测:由于同样的问题,您的'complete_callback'没有被调用,照片在画廊中是不可见的。如果它返回一些奇怪的东西,从应用程序发布日志,否则logcat中的日志就足够了。 – KeyWeeUsr

+0

不,我发现'complete_callback'中的错误在哪里 - 它获取'filename'参数,但照片仍然不在画廊中可见。 – Dzmitry

+0

所有的kivy文件只有在设备重新启动后才会出现。我使用Android 5.0.2的摩托罗拉Moto G。 – Dzmitry

回答

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 5.0.2的摩托罗拉Moto G。

2

所以,我终于解决了问题,画廊,现在我的代码如下所示:

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 

我希望这可以帮助别人。