Dzmitry
Dzmitry

Reputation: 173

Kivy Plyer Camera

I try to make little app with Plyer Camera.

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

But:

  1. When i'm take a shot, the photo is not visible in the gallery on the device and it appears only after the device is reset.
  2. Function complete_callback is not called.

Upvotes: 3

Views: 2388

Answers (2)

Dzmitry
Dzmitry

Reputation: 173

So, i finally solved problem with gallery and now my code looks like this:

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

I hope this helps someone.

Upvotes: 3

Dzmitry
Dzmitry

Reputation: 173

I solved one problem. Function complete_callback must takes a parameter filename, i fix that, and now all works.

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

But, all kivy files appears only after the device was restarted and i think that problem in my device. I use Motorola Moto G with Android 5.0.2.

Upvotes: 1

Related Questions