调用Camera.takePicture(null, null, pictureCallback);拍照并保存于sd卡

package com.ygy;


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;


import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;


public class MyCameraActivity extends Activity {
    /** Called when the activity is first created. */

private CameraView cv;
private Camera mCamera = null;
private Bitmap mBitmap = null;

private int mScreenWidth;
private int mScreenHeight;

public Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {


@Override
public void onPictureTaken(byte[] data, Camera camera) {
// TODO Auto-generated method stub
Log.i("ygy", "onPictureTaken");
Toast.makeText(getApplicationContext(), "正在保存...", Toast.LENGTH_LONG).show();
mBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
File file = new File("/storage/sdcard0/360/01.jpg");
try {
file.createNewFile();
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(file));
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
Toast.makeText(getApplicationContext(), "图像保存成功", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        mScreenWidth = dm.widthPixels;
        mScreenHeight = dm.heightPixels;
        Log.i("ping", mScreenWidth+"");
        Log.i("ping", mScreenHeight+"");
        
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        FrameLayout fl = new FrameLayout(this);
        cv = new CameraView(this);
        fl.addView(cv);
        TextView tv = new TextView(this);
        tv.setText("请拍摄");
        fl.addView(tv);
        setContentView(fl);
    }
    
    
    
    
    /* (non-Javadoc)
* @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub

Log.i("ygy", "onKeyDown");
if(keyCode==KeyEvent.KEYCODE_MENU) {
if(mCamera!=null) {
Log.i("ygy", "mCamera.takePicture");
mCamera.takePicture(null, null, pictureCallback);
}
}

return super.onKeyDown(keyCode, event);
}








class CameraView extends SurfaceView {
   
    private SurfaceHolder holder = null;


public CameraView(Context context) {
super(context);
// TODO Auto-generated constructor stub

Log.i("ygy", "CameraView");
holder = this.getHolder();
holder.addCallback(new SurfaceHolder.Callback() {

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
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) {
// TODO Auto-generated method stub
Camera.Parameters param = mCamera.getParameters();
param.setPictureFormat(PixelFormat.JPEG);
param.setPreviewSize(width, height);
param.setPictureSize(width, height);

mCamera.setParameters(param);
mCamera.startPreview();
}
});
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
   
    }
}

你可能感兴趣的:(调用Camera.takePicture(null, null, pictureCallback);拍照并保存于sd卡)