package
com.hipikids.mycamerademo;
import
java.io.BufferedOutputStream;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
android.app.Activity;
import
android.content.pm.ActivityInfo;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.PixelFormat;
import
android.hardware.Camera;
import
android.hardware.Camera.AutoFocusCallback;
import
android.hardware.Camera.PictureCallback;
import
android.os.Bundle;
import
android.view.SurfaceHolder;
import
android.view.SurfaceView;
import
android.view.View;
import
android.view.Window;
import
android.view.SurfaceHolder.Callback;
import
android.view.View.OnClickListener;
/**
* * @title MainActivity.java
*
* @package com.hipikids.mycamerademo
* <a href="\"http://www.eoeandroid.com/home.php?mod=space&uid=100488\"" target="\"_blank\"">@email</a> <a href="\"mailto:[email protected]\"" target="\"_blank\"">[email protected]</a>
* @author szh
* <a href="\"http://www.eoeandroid.com/home.php?mod=space&uid=572753\"" target="\"_blank\"">@update</a> 2013-1-10 下午5:21:00
* @description
*/
public
class
MainActivity
extends
Activity
implements
Callback,
OnClickListener, AutoFocusCallback {
SurfaceView mySurfaceView;
SurfaceHolder holder;
Camera myCamera;
String filePath =
"/sdcard/wjh.jpg"
;
boolean
isClicked =
false
;
PictureCallback jpeg =
new
PictureCallback() {
@Override
public
void
onPictureTaken(
byte
[] data, Camera camera) {
try
{
Bitmap bm = BitmapFactory.decodeByteArray(data,
0
, data.length);
File file =
new
File(filePath);
BufferedOutputStream bos =
new
BufferedOutputStream(
new
FileOutputStream(file));
bm.compress(Bitmap.CompressFormat.JPEG,
100
, bos);
bos.flush();
bos.close();
}
catch
(Exception e) {
e.printStackTrace();
}
}
};
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
this
.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_main);
mySurfaceView = (SurfaceView) findViewById(R.id.surface);
holder = mySurfaceView.getHolder();
holder.addCallback(
this
);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mySurfaceView.setOnClickListener(
this
);
}
@Override
public
void
surfaceChanged(SurfaceHolder holder,
int
format,
int
width,
int
height) {
Camera.Parameters params = myCamera.getParameters();
params.setPictureFormat(PixelFormat.JPEG);
params.setPreviewSize(
640
,
480
);
myCamera.setParameters(params);
myCamera.startPreview();
}
@Override
public
void
surfaceCreated(SurfaceHolder holder) {
if
(myCamera ==
null
) {
myCamera = Camera.open();
try
{
myCamera.setPreviewDisplay(holder);
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
@Override
public
void
surfaceDestroyed(SurfaceHolder holder) {
myCamera.stopPreview();
myCamera.release();
myCamera =
null
;
}
@Override
public
void
onClick(View v) {
if
(!isClicked) {
myCamera.autoFocus(
this
);
isClicked =
true
;
}
else
{
myCamera.startPreview();
isClicked =
false
;
}
}
@Override
public
void
onAutoFocus(
boolean
success, Camera camera) {
if
(success) {
Camera.Parameters params = myCamera.getParameters();
params.setPictureFormat(PixelFormat.JPEG);
params.setPreviewSize(
640
,
480
);
myCamera.setParameters(params);
myCamera.takePicture(
null
,
null
, jpeg);
}
}
}