1.Activity:
package com.ccas.roadsideconnect;import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;public class Takephotos extends Activity {
private CameraView cv;
private String photoPath;
private Camera mCamera = null;public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// Matrix matrix = new Matrix();
// matrix.postScale(0.05f, 0.05f);
// Bitmap newBitmap=Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
File myCaptureFile = new File(getFilesDir(), photoPath);
try {
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(
myCaptureFile));
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
resetCamera();Intent intent = new Intent();
intent.putExtra("photopath", myCaptureFile.getPath());
setResult(RESULT_OK, intent);
finish();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (mBitmap != null) {
mBitmap.recycle();
}
}
}
};//reret Camera
private void resetCamera() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.takephoto);
// cv = new CameraView(this);
// FrameLayout fl = new FrameLayout(this);
// fl.addView(cv);
// setContentView(fl);
LinearLayout ll=(LinearLayout) findViewById(R.id.cameralayout);
cv = new CameraView(this);
ll.addView(cv);
Bundle extras=getIntent().getExtras();
if(extras!=null){
if(extras.getString("driver")!=null){
photoPath=extras.getString("driver");
}
else if(extras.getString("truck")!=null){
photoPath=extras.getString("truck");
}
}
//
ImageButton take=(ImageButton) findViewById(R.id.take);
take.getBackground().setAlpha(100);
take.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mCamera != null) {
mCamera.takePicture(null, null, pictureCallback);
}
}});
// ImageButton cancel=(ImageButton) findViewById(R.id.cancel);
// cancel.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// finish();
// }
// });
}public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
if (mCamera != null) {
mCamera.takePicture(null, null, pictureCallback);
}
} if(keyCode==KeyEvent.KEYCODE_BACK){
finish();
}
return cv.onKeyDown(keyCode, event);
}
/*
* (non-Javadoc)
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
super.onPause();
resetCamera();
}// set the Camera
class CameraView extends SurfaceView {private SurfaceHolder holder = null;
public CameraView(Context context) {
super(context);
holder = this.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
mCamera.release();
mCamera = null;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = mCamera.getParameters();
// Set images format
parameters.setPictureFormat(PixelFormat.JPEG);
// set Preview size
// WindowManager manager=(WindowManager) getSystemService(Context.WIFI_SERVICE);
// Display display=manager.getDefaultDisplay();
// parameters.setPreviewSize(320, 480);
// parameters.setPreviewSize(display.getWidth(), display.getHeight());
//set automatic focus
// parameters.setFocusMode(Parameters.FOCUS_MODE_AUTO);
// Setting the picture the resolution of the preservation size
//parameters.setPictureSize(320, 240);
// set the data of Camera
mCamera.setParameters(parameters);
// start preview
mCamera.startPreview();
}
});
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}}
}
2.布局文件
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"><LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/cameralayout" /><LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:gravity="bottom|center" android:layout_gravity="bottom|center">
<ImageButton android:id="@+id/take" android:src="@drawable/takephoto"
android:layout_width="100dp" android:layout_height="50dp" />
<!--
<ImageButton android:id="@+id/cancel" android:src="@drawable/takephoto"
android:layout_width="100dp" android:layout_height="50dp" />
-->
</LinearLayout>
</FrameLayout>