1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
| class ImageSender implements Runnable{
final private int limit = 5;
int current = 0;
boolean alive=true;
synchronized void received(){
current--;
Log.d("PPK","notifay");
this.notify();
}
synchronized void reset(){
current=0;
alive=false;
this.notify();
}
synchronized void alive(){
alive=true;
}
@Override
public void run() {
Log.d("ImageSender", "starting thread");
try{
while(true){
Log.d("ImageSender", "looping");
while(alive && current<limit){
Log.d("ImageSender", "looping OK");
Log.d("PPK","looping with current" + current);
current++;
mCamera = getCameraInstance();
try {
mCamera.takePicture(null, null, null, mPicture);
} catch(Throwable e) {
Log.d("ERROR", "Throwable "+e.getMessage());
Log.d("ERROR", "Throwable2 "+e.toString());
}
try {
Thread.sleep(1050); //50
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
synchronized (this) {
this.wait(1020);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}catch (Throwable t) {
// TODO: handle exception
Log.d("EX", "expection out of memory ?"+t );
t.printStackTrace();
clientState = new MyWebSocketListener();
}
}
}
private String TAG = "HTTPCamera";
private CameraSurfaceView cameraSurfaceView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
clientState = new MyWebSocketListener();
requestWindowFeature(Window.FEATURE_NO_TITLE);
Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();
}
private Camera mCamera;
private PicPreview mPreview;
private final static String TAG2 = "MainActivity";
private int mCount;
private FrameLayout preview;
//private SocketIO client;
@Override
protected void onResume() {
super.onResume();
setContentView(R.layout.activity_main);
// Create an instance of Camera
mCamera = getCameraInstance();
Log.d(TAG2, "setting client done");
mPreview = new PicPreview(this, mCamera);
preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(cameraSurfaceView);
Log.d(TAG, "setting preview done");
}
public void showToast(final String toast)
{
runOnUiThread(new Runnable() {
public void run()
{
Toast.makeText(MainActivity.this, toast, Toast.LENGTH_LONG).show();
}
});
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] byteData, Camera camera) {
Log.d(TAG2, "event: picture beginning");
Log.d(TAG2, "byte " + byteData);
Log.d(TAG2, "client: " + clientState);
clientState.send(byteData);
Log.d(TAG2, "event: picture sent");
}
};
@Override
public void onPause() {
super.onPause(); // Always call the superclass method first
// Release the Camera because we don't need it when paused
// and other activities might need to use it.
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
@Override
protected void onStop() {
super.onStop(); // Always call the superclass method first
}
@Override
protected void onStart() {
super.onStart(); // Always call the superclass method first
}
@Override
protected void onRestart() {
super.onRestart(); // Always call the superclass method first
// Activity being restarted from stopped state
}
public Camera getCameraInstance(){
if (mCamera != null) {
return mCamera;
}
int cameraId=0;
int cameraCount = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
cameraCount = Camera.getNumberOfCameras();
for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {
Camera.getCameraInfo( camIdx, cameraInfo );
if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
mCamera = Camera.open( camIdx );
Log.d(TAG2, "cameraId: " + cameraId);
return mCamera;
} catch (RuntimeException e) {
Log.e(TAG2, "Camera failed to open: " + e.getLocalizedMessage());
}
}
}
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
} |
Partager