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).

!7

import android.app.Activity;
import android.widget.*;
import android.graphics.*;
import android.hardware.Camera;
import android.view.*;

p {
  print("Listing cameras...");
  
  runOnUiThread(r {
    int n = Camera.getNumberOfCameras();
    System.out.println("Cameras: " + n);

    final Camera cam = Camera.open(0/*n-1*/);
    
    Camera.Parameters p = cam.getParameters();
    p.setPreviewSize(100, 100);
    cam.setParameters(p);

    SurfaceView surfaceView = new SurfaceView(androidContext());
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    surfaceHolder.addCallback(new CamCallback());
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
    androidShow(surfaceView);
    
    cam.setPreviewDisplay(surfaceHolder);
    cam.startPreview();
    
    print("Preview started.");
    
    surfaceView.post(r {
      cam.takePicture(null, null, null, new JpegCallback());
    });
    
    print("Waiting for callback");
  });
}

static class CamCallback implements SurfaceHolder.Callback {
  public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {
    print("surfaceChanged");
  }
  public void surfaceCreated (SurfaceHolder holder) {
    print("surfaceCreated");
  }
  public void surfaceDestroyed (SurfaceHolder holder) {
    print("surfaceDestroyed");
  }
}

static class ShutterCallback implements Camera.ShutterCallback {
  public void onShutter() {
    print("Shutter!");
  }
}

static class JpegCallback implements Camera.PictureCallback {
  public void onPictureTaken (byte[] data, Camera cam) {
    TextView tv = aTextView();
    tv.setText("Picture data: " + data.length);
    androidShow(tv);
    
    System.out.println("Picture data: " + data.length);
    cam.release();
    print("Camera released.");
  }
}

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: 288 / 396
Referenced in: [show references]