Hello all!
I am trying to create an Activity which handles all needs of PictureCapture for my application. A single activity which is called from multiple places and it returns(via CallSubDelayed) the captured picture(the path-to-it).
Capturing works fine, however I have the following problem. I am trying to immitate the design of other applications where you take a picture but it gives you the option to take a new one if you don't like the results.
What doesn't work is - when I take a picture, then click "btnTakeNew", I don't get preview on my panel again. It just gets empty(because I call "clearPanel" method to remove the snapshot when I am going to show the preview again.
A more interesting thing is, no matter I don't get a preview, if I click on the panel, a new image is taken and the new image correspond to the location I've directed my phone to. So the pictures are real but you don't see where you are aiming. It works the first time I open the activity but doesn't work after I don't like the result and go for "Take New"(click the button).
My code pretty much copies the Camera example. I've just added Subs because I hate working with events directly.
I am trying to create an Activity which handles all needs of PictureCapture for my application. A single activity which is called from multiple places and it returns(via CallSubDelayed) the captured picture(the path-to-it).
Capturing works fine, however I have the following problem. I am trying to immitate the design of other applications where you take a picture but it gives you the option to take a new one if you don't like the results.
What doesn't work is - when I take a picture, then click "btnTakeNew", I don't get preview on my panel again. It just gets empty(because I call "clearPanel" method to remove the snapshot when I am going to show the preview again.
A more interesting thing is, no matter I don't get a preview, if I click on the panel, a new image is taken and the new image correspond to the location I've directed my phone to. So the pictures are real but you don't see where you are aiming. It works the first time I open the activity but doesn't work after I don't like the result and go for "Take New"(click the button).
My code pretty much copies the Camera example. I've just added Subs because I hate working with events directly.
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
'Dim out As OutputStream
End Sub
Sub Globals
Dim camera1 As Camera
Dim btnTakePicture As Button
Dim came_from As Object
Dim backSub As String
Dim pnlCameraImage As Panel
Dim btnOK As Button
Dim btnTakeNew As Button
Dim pnlControls As Panel
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layCamera_takephoto")
End Sub
Sub Activity_Resume
initializeCamera
End Sub
Sub Activity_Pause (UserClosed As Boolean)
camera1.Release
End Sub
Sub Activity_KeyUp (KeyCode As Int) As Boolean
If(KeyCode = KeyCodes.KEYCODE_DPAD_CENTER) Then
TakePicture
End If
End Sub
Sub selector(sourcePlace As Object)
came_from = sourcePlace
'initializeCamera
End Sub
Sub initializeCamera()
Activity.Invalidate
Try
' camera1.Release
Catch
End Try
Try
camera1.Initialize(pnlCameraImage, "Camera1")
Catch
End Try
hideControlsPanel
End Sub
Sub getBack()
If(backSub = "") Then
CallSubDelayed(came_from, "getBackWithPicture")
Else
CallSubDelayed(came_from, backSub)
End If
End Sub
Sub Camera1_Ready (Success As Boolean)
If Success Then
StartRecording
'btnTakePicture.Enabled = True
Else
ToastMessageShow("Cannot open camera.", True)
End If
End Sub
Sub Camera1_PictureTaken (Data() As Byte)
'camera1.StartPreview
Dim out As OutputStream
out = File.OpenOutput(File.DirRootExternal, "temp.jpg", False)
out.WriteBytes(Data, 0, Data.Length)
out.Close
ToastMessageShow("Image saved: " & File.Combine(File.DirDefaultExternal, "temp.jpg"), True)
Dim img As ImageView
img.Initialize("img")
img.Bitmap = LoadBitmap(File.DirRootExternal, "temp.jpg")
pnlCameraImage.AddView(img, 0, 0, 100%x, 100%y) ' add a snapshot to the panel.
showControlsPanel ' show the buttons btnOK and btnTakeNew
End Sub
Sub TakePicture()
camera1.TakePicture ' take the picture
StopRecording ' stop recording. I think this is handled internally but not sure.
End Sub
' starts the previous of images
Sub StartRecording()
pnlControls.Visible = False ' hide the btnOK and btnTakeNew controls
camera1.StartPreview
End Sub
Sub StopRecording()
camera1.StopPreview
End Sub
Sub showControlsPanel()
pnlControls.Visible = True
End Sub
Sub hideControlsPanel()
pnlControls.Visible = False
End Sub
Sub btnTakeNew_Click()
zCommon.clearPanel(pnlCameraImage) ' clear the snapshot
StartRecording ' start new previews
End Sub
Sub btnOK_Click
getBack ' we like the result, go back to the calling Activity
End Sub
Sub pnlCameraImage_Click
If(pnlControls.Visible = True) Then ' this will disable picture taking if the controls menu is visible. It is time for decision, not new picture taking.
Return True
End If
TakePicture
End Sub