Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

70
LINES

< > BotCompany Repo | #1015068 // Android: Take picture (seems to work!)

JavaX source code (Android) [tags: use-pretranspiled] - run with: the app

Libraryless. Click here for Pure Java version (1818L/12K/46K).

1  
!7
2  
3  
import android.app.Activity;
4  
import android.widget.*;
5  
import android.graphics.*;
6  
import android.hardware.Camera;
7  
import android.view.*;
8  
9  
p {
10  
  print("Listing cameras...");
11  
  
12  
  runOnUiThread(r {
13  
    int n = Camera.getNumberOfCameras();
14  
    System.out.println("Cameras: " + n);
15  
16  
    final Camera cam = Camera.open(0/*n-1*/);
17  
    
18  
    Camera.Parameters p = cam.getParameters();
19  
    p.setPreviewSize(100, 100);
20  
    cam.setParameters(p);
21  
22  
    SurfaceView surfaceView = new SurfaceView(androidContext());
23  
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
24  
    surfaceHolder.addCallback(new CamCallback());
25  
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
26  
    
27  
    androidShow(surfaceView);
28  
    
29  
    cam.setPreviewDisplay(surfaceHolder);
30  
    cam.startPreview();
31  
    
32  
    print("Preview started.");
33  
    
34  
    surfaceView.post(r {
35  
      cam.takePicture(null, null, null, new JpegCallback());
36  
    });
37  
    
38  
    print("Waiting for callback");
39  
  });
40  
}
41  
42  
static class CamCallback implements SurfaceHolder.Callback {
43  
  public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {
44  
    print("surfaceChanged");
45  
  }
46  
  public void surfaceCreated (SurfaceHolder holder) {
47  
    print("surfaceCreated");
48  
  }
49  
  public void surfaceDestroyed (SurfaceHolder holder) {
50  
    print("surfaceDestroyed");
51  
  }
52  
}
53  
54  
static class ShutterCallback implements Camera.ShutterCallback {
55  
  public void onShutter() {
56  
    print("Shutter!");
57  
  }
58  
}
59  
60  
static class JpegCallback implements Camera.PictureCallback {
61  
  public void onPictureTaken (byte[] data, Camera cam) {
62  
    TextView tv = aTextView();
63  
    tv.setText("Picture data: " + data.length);
64  
    androidShow(tv);
65  
    
66  
    System.out.println("Picture data: " + data.length);
67  
    cam.release();
68  
    print("Camera released.");
69  
  }
70  
}

Author comment

Began life as a copy of #1000431

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, nbgitpuheiab, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015068
Snippet name: Android: Take picture (seems to work!)
Eternal ID of this version: #1015068/1
Text MD5: cf8dca3dcdd18df2ca03f0b562a4c76a
Transpilation MD5: 018b53b80facacdf709bea034366f9f7
Author: stefan
Category: javax android
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-07 18:12:01
Source code size: 1862 bytes / 70 lines
Pitched / IR pitched: No / No
Views / Downloads: 290 / 399
Referenced in: [show references]