你可以尝试这样的事情,但只是一个粗略的代码给你方向。
Bitmap bmp;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//set view
setContentView(R.layout.main);
preview = new Preview(this); // <3>
rootLayout = ((FrameLayout) findViewById(R.id.preview));
rootLayout.addView(preview);
progress = new ProgressDialog(this);
ImageView rectView = (ImageView) findViewById(R.id.imageView1);
rectView.bringToFront();
buttonClick = (Button) findViewById(R.id.buttonClick);
buttonClick.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) { // <5>
new connectAsyncTask.execute();
buttonClick.setEnabled(false);
}
});
}
public void comparison(byte[] data,Camera camera) throws IOException
{
String path=getString(R.string._sdcard_path);//<string name="_sdcard_path">/sdcard/images/</string>
String extension=getString(R.string._sdcard_d_jpg);// <string name="_sdcard_d_jpg">.jpg</string>
/*String srcPath1="/sdcard/images/";
File f=new File(srcPath1);
int h=0,count=0;
String [] cnt=f.list();
while(h<cnt.length)
{
count++;
h++;
}extension=count+extension;*/
ByteArrayOutputStream stream=null;
FileOutputStream outStream = null;
bmp =BitmapFactory.decodeByteArray(data, 0, data.length);
// int i=0,x=50;
stream=new ByteArrayOutputStream();
temBitmap.compress(Bitmap.CompressFormat.JPEG,100, stream);
path+=String.format(
getString(R.string._sdcard_d_jpg),
System.currentTimeMillis());
outStream = new FileOutputStream(path+extension); // <9>
outStream.write(stream.toByteArray());
outStream.close();
//rs="Place the strip correctly!!";
Toast.makeText(getApplicationContext(),"Test", Toast.LENGTH_LONG).show();
}
// Called when shutter is opened
ShutterCallback shutterCallback = new ShutterCallback() { // <6>
@Override
public void onShutter() {
Log.d(TAG, "onShutter'd");
}
};
// Handles data for raw picture
PictureCallback rawCallback = new PictureCallback() { // <7>
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.d(TAG, "onPictureTaken - raw");
}
};
// Handles data for jpeg picture
PictureCallback jpegCallback = new PictureCallback() { // <8>
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
comparison(data,camera);//new connectAsyncTask(data, camera).execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
void showProgress() {
//Toast.makeText(getApplicationContext()," Picture is saving please wait..!!", Toast.LENGTH_SHORT).show();
progress.setIndeterminate(true);
progress.setCancelable(false);
progress.setCanceledOnTouchOutside(false);
progress.show();
}
private class connectAsyncTask extends AsyncTask<Void, Void, String> {
// private ProgressDialog progressDialog;
connectAsyncTask(byte[] data, Camera camera) throws IOException {
//saveImageToSd(data, camera);
//Toast.makeText(getApplicationContext(), stat, Toast.LENGTH_LONG).show();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
showProgress();
}
@Override
protected String doInBackground(Void... params) {
preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback);
comparison(data,camera);
return "";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progress.dismiss();
ImageView rectView = (ImageView) findViewById(R.id.imageView1);
rectView.setImageBitmap(bmp);
}
}
@Override
protected void onDestroy() {
Parameters params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
if(camera!=null){
camera.stopPreview();
camera.release();
camera = null;
}
}