Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.CameraFirstTest;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.widget.LinearLayout;
- import net.yeputons.robotics.libs.CameraData;
- import net.yeputons.robotics.libs.CameraListener;
- import net.yeputons.robotics.libs.CameraView;
- import java.nio.Buffer;
- public class MyActivity extends Activity {
- /**
- * Called when the activity is first created.
- */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- CameraView view = new CameraView(this);
- ((LinearLayout)findViewById(R.id.linearLayout)).addView(view);
- view.setDataRotation(CameraView.DATA_ROTATION_AUTO);
- view.setCameraListener(new CameraListener() {
- private Bitmap bmp;
- private int[] pixels;
- @Override
- public void onCameraFrame(CameraData data, int cameraDisplayOrientation, Canvas canvas) {
- for (int y = 0; y < data.getHeight() / 2; y++)
- for (int x = 0; x < data.getWidth() / 4; x++) {
- int c = data.getColor(x, y);
- int val = 0;
- val += c & 0xFF;
- val += (c >> 8) & 0xFF;
- val += (c >> 16) & 0xFF;
- val /= 3;
- pixels[y * data.getWidth() + x] = (val >= 128) ? Color.WHITE : Color.BLACK;
- }
- bmp.setPixels(pixels, 0, data.getWidth(), 0, 0, data.getWidth(), data.getHeight());
- Paint myPaint = new Paint();
- canvas.drawBitmap(bmp, 0, 0, myPaint);
- }
- @Override
- public void onCameraFrame(byte[] data, int width, int height, int cameraDisplayOrientation, Canvas canvas) {
- }
- @Override
- public void onSizeChange(int width, int height, int cameraDisplayOrientation) {
- Log.d("onSizeChange", String.format("%d x %d; %d", width, height, cameraDisplayOrientation));
- if (bmp != null) {
- bmp.recycle();
- }
- bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
- pixels = new int[width * height];
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement