Advertisement
Guest User

Untitled

a guest
Jul 25th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.66 KB | None | 0 0
  1. package com.example.CameraFirstTest;
  2.  
  3. import android.app.Activity;
  4. import android.graphics.Bitmap;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.hardware.Sensor;
  9. import android.hardware.SensorEvent;
  10. import android.hardware.SensorEventListener;
  11. import android.hardware.SensorManager;
  12. import android.os.Bundle;
  13. import android.util.Log;
  14. import android.widget.LinearLayout;
  15. import net.yeputons.robotics.libs.CameraData;
  16. import net.yeputons.robotics.libs.CameraListener;
  17. import net.yeputons.robotics.libs.CameraView;
  18.  
  19. import java.nio.Buffer;
  20.  
  21. public class MyActivity extends Activity {
  22.     /**
  23.      * Called when the activity is first created.
  24.      */
  25.     @Override
  26.     public void onCreate(Bundle savedInstanceState) {
  27.         super.onCreate(savedInstanceState);
  28.         setContentView(R.layout.main);
  29.  
  30.         CameraView view = new CameraView(this);
  31.         ((LinearLayout)findViewById(R.id.linearLayout)).addView(view);
  32.         view.setDataRotation(CameraView.DATA_ROTATION_AUTO);
  33.  
  34.         view.setCameraListener(new CameraListener() {
  35.             private Bitmap bmp;
  36.             private int[] pixels;
  37.  
  38.             @Override
  39.             public void onCameraFrame(CameraData data, int cameraDisplayOrientation, Canvas canvas) {
  40.                 for (int y = 0; y < data.getHeight() / 2; y++)
  41.                     for (int x = 0; x < data.getWidth() / 4; x++) {
  42.                         int c = data.getColor(x, y);
  43.                         int val = 0;
  44.                         val += c & 0xFF;
  45.                         val += (c >> 8) & 0xFF;
  46.                         val += (c >> 16) & 0xFF;
  47.                         val /= 3;
  48.                         pixels[y * data.getWidth() + x] = (val >= 128) ? Color.WHITE : Color.BLACK;
  49.                     }
  50.                 bmp.setPixels(pixels, 0, data.getWidth(), 0, 0, data.getWidth(), data.getHeight());
  51.                 Paint myPaint = new Paint();
  52.                 canvas.drawBitmap(bmp, 0, 0, myPaint);
  53.             }
  54.  
  55.             @Override
  56.             public void onCameraFrame(byte[] data, int width, int height, int cameraDisplayOrientation, Canvas canvas) {
  57.  
  58.             }
  59.  
  60.             @Override
  61.             public void onSizeChange(int width, int height, int cameraDisplayOrientation) {
  62.                 Log.d("onSizeChange", String.format("%d x %d; %d", width, height, cameraDisplayOrientation));
  63.                 if (bmp != null) {
  64.                     bmp.recycle();
  65.                 }
  66.                 bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  67.                 pixels = new int[width * height];
  68.             }
  69.         });
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement