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

86
LINES

< > BotCompany Repo | #1000447 // Android: Open camera v2 (developing)

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

Libraryless. Click here for Pure Java version (95L/1K/5K).

!636

!711 // runnable {

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

main {
  static Activity androidContext;
  
  psvm {
    print "Listing cameras..."
    
    androidContext.runOnUiThread(runnable {
      int n = Camera.getNumberOfCameras();
      System.out.println("Cameras: " + n);
  
      //final Camera cam = Camera.open(0/*n-1*/);
      
      TextureView mTextureView = new TextureView(androidContext);
      
      mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
        Camera mCamera;
        
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            mCamera = Camera.open(0);
  
            try {
                mCamera.setPreviewTexture(surface);
                mCamera.startPreview();
            } catch (IOException ioe) {
                // Something bad happened
            }
        }
  
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
            // Ignored, Camera does all the work for us
        }
  
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            mCamera.stopPreview();
            mCamera.release();
            return true;
        }
  
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {
            // Invoked every time there's a new Camera preview frame
        }        
      });
      
      androidContext.setContentView(mTextureView);
    });
  }
  
  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 = new TextView(androidContext);
      tv.setText("Picture data: " + data.length);
      androidContext.setContentView(tv);
      
      System.out.println("Picture data: " + data.length);
      cam.release();
      print "Camera released."
    }
  } 
}

Author comment

Began life as a copy of #1000431

From example: http://developer.android.com/reference/android/view/TextureView.html

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000447
Snippet name: Android: Open camera v2 (developing)
Eternal ID of this version: #1000447/1
Text MD5: 43254d024c1fea58cdb39cb7b13d5e3f
Transpilation MD5: 2748d48c90dd85041bddcdb41a10657c
Author: stefan
Category: javax android
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-07 15:29:40
Source code size: 2568 bytes / 86 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 600 / 588
Referenced in: [show references]