Code example for Bitmap

Methods: compresscreateBitmapgetHeightgetWidthrecycle

0
import android.database.Cursor; 
import android.graphics.Bitmap; 
import android.graphics.Bitmap.CompressFormat; 
import android.graphics.BitmapFactory; 
import android.media.ExifInterface; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.provider.MediaStore.Images; 
import android.provider.MediaStore.Images.Media; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.ImageView; 
import android.widget.Toast; 
import android.view.Window; 
 
public class StereoVision extends Activity {
	private static final String IMAGE_DIRECTORY = "/sdcard/DCIM/Camera";
	private static final int ACTIVITY_SELECT_CAMERA = 0;
	private static final int ACTIVITY_SELECT_IMAGE = 1;
	private static final String TAG = "StereoVision::Main";
	private static final int CAMERA_ID = Menu.FIRST;
	private static final int GALLERY_ID = Menu.FIRST + 1;
	private String mCurrentImagePath = null;
 
	private OpenCVView opencv = new OpenCVView();
	private ImageView mImageView;
 
    private Preview mPreview;
    Camera mCamera;
    int numberOfCameras;
    int cameraCurrentlyLocked;
	 
    // The first rear facing camera 
    int defaultCameraId;
 
    public StereoVision() { 
        Log.i(TAG, "Instantiated new " + this.getClass());
    } 
 
	@Override 
	public void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "onCreate");
		super.onCreate(savedInstanceState);
        // requestWindowFeature(Window.FEATURE_NO_TITLE); 
		// mImageView = new ImageView(this); 
		// setContentView(mImageView); 
 
        // // Hide the window title. 
        // requestWindowFeature(Window.FEATURE_NO_TITLE); 
        // getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
 
		// setContentView(new OpenCVView(this)); 
		mPreview = new Preview(this);
		setContentView(mPreview);
 
        // Find the total number of cameras available 
        numberOfCameras = Camera.getNumberOfCameras();
 
        // Find the ID of the default camera 
        CameraInfo cameraInfo = new CameraInfo();
		for (int i = 0; i < numberOfCameras; i++) {
			Camera.getCameraInfo(i, cameraInfo);
			if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
				defaultCameraId = i;
			} 
		} 
 
        Log.i(TAG, "onCreate - finish");
	} 
 
    @Override 
	protected void onResume() { 
        super.onResume(); 
 
        // Open the default i.e. the first rear facing camera. 
        mCamera = Camera.open();
        cameraCurrentlyLocked = defaultCameraId;
        mPreview.setCamera(mCamera);
    } 
 
    @Override 
	protected void onPause() { 
        super.onPause(); 
 
        // Because the Camera object is a shared resource, it's very 
        // important to release it when the activity is paused. 
        if (mCamera != null) {
            mPreview.setCamera(null);
            mCamera.release();
            mCamera = null;
        } 
    } 
 
	@Override 
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, CAMERA_ID, 0, "Camera");
		menu.add(0, GALLERY_ID, 0, "Gallery");
		return true; 
	} 
 
	@Override 
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
		case CAMERA_ID:
			Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			long timeTaken = System.currentTimeMillis();
			mCurrentImagePath = IMAGE_DIRECTORY + "/"
					+ Utility.createName(timeTaken) + ".jpg";
			Log.i(TAG, mCurrentImagePath);
			cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,
					Uri.fromFile(new File(mCurrentImagePath)));
			startActivityForResult(cameraIntent, ACTIVITY_SELECT_CAMERA);
			return true; 
		case GALLERY_ID:
			Intent galleryIntent = new Intent(Intent.ACTION_PICK,
					Images.Media.INTERNAL_CONTENT_URI);
			startActivityForResult(galleryIntent, ACTIVITY_SELECT_IMAGE);
			return true; 
		} 
 
		return super.onMenuItemSelected(featureId, item);
	} 
 
	@Override 
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == ACTIVITY_SELECT_CAMERA
				&& resultCode == Activity.RESULT_OK) {
			ContentValues values = new ContentValues();
			int degrees = Utility.getRotationFromImage(mCurrentImagePath);
			try { 
				ExifInterface exif = new ExifInterface(mCurrentImagePath);
				float[] position = new float[2];
				if (exif.getLatLong(position)) {
					values.put(Images.Media.LATITUDE, position[0]);
					values.put(Images.Media.LONGITUDE, position[1]);
				} 
			} catch (Exception e) {
 
			} 
			// reduce the size of image 
			try { 
				BitmapFactory.Options option = new BitmapFactory.Options();
				option.inSampleSize = 4;
				Bitmap bitmap = BitmapFactory.decodeFile(mCurrentImagePath,
						option);
				if (degrees != 0) {
					bitmap = Utility.rotate(bitmap, degrees);
				} 
				FileOutputStream out = new FileOutputStream(mCurrentImagePath);
				bitmap.compress(CompressFormat.JPEG, 100, out);
			} catch (Exception e) {
 
			} 
			values.put(Images.Media.MIME_TYPE, "image/jpeg");
			values.put(Images.Media.DATA, mCurrentImagePath);