|
|
Press the "Take a Picture" button and Android camera tool appears. The button initiates intent MediaStore.ACTION_IMAGE_CAPTURE for variable rawImagePathFile which is fixed to File(Environment.getExternalStorageDirectory(), "image.jpg"); The image file is created at "/mnt/sdcard/image.jpg" where you can explore it using DDMS. An InputStream bitmap is for the image file is captured and assigned to the ImageView. A target directory is created to store a compressed bitmap image size in form of a jpeg. A png file is considerably larger at five times the size of a jpeg and the percent compressed scale size is ignored so a jpeg is a preferred image format for this POC. The target image file is saved with a System.currentTimeMillis() filename.
package com.brainyideas.dev.poc.bpct;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.FileInputStream;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
public class CameraTake extends Activity {
private static final int REQUEST_IMAGE = 100;
Button captureButton;
ImageView imageView;
File rawImagePathFile;
String targetImagePath;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
void init() {
captureButton = (Button) findViewById(R.id.capture);
captureButton.setOnClickListener(listener);
imageView = (ImageView) findViewById(R.id.image);
rawImagePathFile = new File(Environment.getExternalStorageDirectory(),"image.jpg");
targetImagePath = Environment.getExternalStorageDirectory().toString() + "/camapp";
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK) {
try {
// get the image produced by the OS
InputStream is = new FileInputStream(rawImagePathFile);
// resize the bitmap
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap mBitmap = BitmapFactory.decodeStream(is,null,options);
is.close();
// apply the bitmap to the screen ImageView
Bitmap.createBitmap(mBitmap);
imageView.setImageBitmap(mBitmap);
// create the target directory if it does not exist
File folder = new File(targetImagePath);
if(folder.exists()) {
// ignore
} else {
folder.mkdir(); // returns true/false
}
// save the file compressed for small file size
String filename = String.valueOf(System.currentTimeMillis()) + ".jpg";
File file = new File(targetImagePath, filename);
FileOutputStream fos = new FileOutputStream(file);
mBitmap.compress(CompressFormat.JPEG, 80, fos);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Add extra to save full-image somewhere
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(rawImagePathFile));
Toast.makeText(CameraTake.this, Uri.fromFile(rawImagePathFile).toString(), Toast.LENGTH_SHORT).show();
startActivityForResult(intent, REQUEST_IMAGE);
}
};
}
/*
// this is an alternative bitmap scaler
private Bitmap createScaledBitmapKeepingAspectRatio(Bitmap bitmap, int maxSide) {
int h = bitmap.getHeight();
int w = bitmap.getWidth();
int scaleW = (w >= h) ? maxSide : (int) ((float) maxSide * ((float) w / (float) h));
int scaleH = (h >= w) ? maxSide : (int) ((float) maxSide * ((float) h / (float) w));
Bitmap scalePic = Bitmap.createScaledBitmap(bitmap, scaleW, scaleH, true);
return scalePic;
}
*/
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/capture" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Take a Picture" /> <ImageView android:id="@+id/image" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="centerInside" /> </LinearLayout>