2016-04-28 76 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 

我希望這可以幫助別人。