Class: YesYouCam

Inherits:
Object
  • Object
show all
Defined in:
lib/project/yesyoucam.rb

Constant Summary collapse

OK =
Android::App::Activity::RESULT_OK
CAPTURE_IMAGE_RC =
100
CHOOSE_IMAGE_RC =
101
DATA =

MediaStore.Images.Media.DATA

"_data"

Class Method Summary collapse

Class Method Details



64
65
66
67
68
69
70
# File 'lib/project/yesyoucam.rb', line 64

def add_to_gallery
  media_scan_intent = Potion::Intent.new(Potion::Intent::ACTION_MEDIA_SCANNER_SCAN_FILE)
  photo_file = Potion::File.new(photo_path)
  content_uri = Potion::Uri.fromFile(photo_file)
  media_scan_intent.setData(content_uri)
  find.app.context.sendBroadcast(media_scan_intent)
end

.bmp_data(optional_path = nil) ⇒ Object



55
56
57
58
# File 'lib/project/yesyoucam.rb', line 55

def bmp_data(optional_path=nil)
  file_path = optional_path ? optional_path : photo_path
  Android::Graphics::BitmapFactory.decodeFile(file_path)
end

.camera_exists?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/project/yesyoucam.rb', line 60

def camera_exists?
  find.app.package_manager.hasSystemFeature("android.hardware.camera")
end

.capture_photoObject

Returns true or false on success of firing a photo intent



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/project/yesyoucam.rb', line 14

def capture_photo
  success = false
  if camera_exists?
    # Performing this check is important because if you call startActivityForResult
    # using an intent that no app can handle, your app will crash.
    # So as long as the result is not nil, it's safe to use the intent.
    take_picture_intent = Potion::Intent.new(Potion::MediaStore::ACTION_IMAGE_CAPTURE)
    if take_picture_intent.resolveActivity(app.package_manager)
      # create place for photo to go
      photo_file = create_image_file

      if photo_file
        take_picture_intent.putExtra(Potion::MediaStore::EXTRA_OUTPUT, Potion::Uri.fromFile(photo_file))
        find.activity.startActivityForResult(take_picture_intent, YesYouCam::CAPTURE_IMAGE_RC)
        success = true
      end
    end
  end
  success
end

.choose_photoObject



35
36
37
38
# File 'lib/project/yesyoucam.rb', line 35

def choose_photo
  choose_pic = Potion::Intent.new(Potion::Intent::ACTION_PICK, Potion::INTERNAL_CONTENT_URI)
  find.activity.startActivityForResult(choose_pic, YesYouCam::CHOOSE_IMAGE_RC)
end

.photo_pathObject



51
52
53
# File 'lib/project/yesyoucam.rb', line 51

def photo_path
  @current_photo_path
end

.pic_path_from_uri(content_uri) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/project/yesyoucam.rb', line 72

def pic_path_from_uri (content_uri)
  cursor = find.app.context.contentResolver.query(content_uri, nil, nil, nil, nil)
  column_index = cursor.getColumnIndex(YesYouCam::DATA)
  path = nil
  cursor.moveToFirst

  # Some devices have SD cards/google services that make this a living hell... protect the user!
  if column_index >= 0
    path = cursor.getString(column_index)
  else
    # We're probably on an SD card - check common places (We're not the first one to attempt this terrible idea... DAMN YOU ANDROID!)
    external = Potion::Environment.getExternalStorageDirectory.getAbsolutePath
    dcim = Potion::Environment.getExternalStoragePublicDirectory(Potion::Environment::DIRECTORY_DCIM).getAbsolutePath
    pictures = Potion::Environment.getExternalStoragePublicDirectory(Potion::Environment::DIRECTORY_PICTURES).getAbsolutePath

    potential_dirs = ["#{dcim}/Camera/", "#{dcim}/100Media/", "#{dcim}/100ANDRO/", "#{dcim}/100LGDSC/", "#{external}/Images/", "#{pictures}/", "#{dcim}"]

    potential_dirs.each do |current_path|
      potential_file = current_path + cursor.getString(0)
      path = potential_file if Potion::File.new(potential_file).exists
      break if path #shortcircuit now that we've found it!
    end
  end

  cursor.close

  path
end

.pic_to_png(quality = 100) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/project/yesyoucam.rb', line 40

def pic_to_png(quality=100)
  bmp = Potion::BitmapFactory.decodeFile(@current_photo_path)
  # NOTE the instance var for current photo changes with this command
  create_image_file("png")
  image = Potion::File.new(@current_photo_path)
  out_stream = Potion::FileOutputStream.new(image)
  bmp.compress(Android::Graphics::Bitmap::CompressFormat::PNG, quality, out_stream)
  out_stream.flush
  out_stream.close
end