» 首頁 » 討論區 » Android程式設計 »照相範例按鍵拍照改觸碰拍照

照相範例按鍵拍照改觸碰拍照

發表人: GG
積分: 70
發表時間: 2012-10-27 00:10:33
照書上範例KEY進去是按相機鍵拍照
但是有的手機沒拍照鍵
所以想改觸碰螢幕就可以拍照
上網找了一些不過都很陽春
而且不知從何改起
我不想放棄書上的範例然後重新撰寫......

其中ONTOUCHEVENT那段是自己加的但是都沒有用....

[sea:javaCode]
private final String tag = getClass().getName();
private Camera camera;
private SurfaceView svPreview;
private SurfaceHolder sHolder;
private Bitmap image;
private boolean isPreview = false;


class SurfaceCallback implements SurfaceHolder.Callback{
public void surfaceCreated(SurfaceHolder holder) {
if(camera == null)
camera = Camera.open();
}

public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Camera.Parameters params = camera.getParameters();

List<Size> sizes = params.getSupportedPictureSizes();


Size maxSize = params.getPictureSize();
for(Size size:sizes){
maxSize= maxSize.width * maxSize.height>size.width * size.height?maxSize:size;
}


params.setPictureSize(maxSize.width, maxSize.height);



params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
camera.setParameters(params);
try{

camera.setPreviewDisplay(holder);
startPreview();
}
catch(IOException e){
Log.e(tag, e.toString());
}
}

public void surfaceDestroyed(SurfaceHolder holder) {
stopPreview();
if(camera != null){
camera.release();
camera = null;
}
}
}

class AutoFocusCallback implements Camera.AutoFocusCallback{
public void onAutoFocus(boolean success,Camera camera){
if(success){
camera.takePicture(null,null,jpeg);
isPreview = false;
}
}
}

Camera.PictureCallback jpeg = new Camera.PictureCallback(){
public void onPictureTaken(byte[] imgData, Camera camera){
if(imgData != null){
BitmapFactory.Options options = new BitmapFactory.Options();


options.inSampleSize = 1;
image = BitmapFactory.decodeByteArray(imgData, 0, imgData.length, options);
}
}
};


public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setFullScreen();
setContentView(R.layout.page5);
findViews();
}

private void setFullScreen() {

requestWindowFeature(Window.FEATURE_NO_TITLE);


getWindow().setFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}

private void findViews() {
svPreview = (SurfaceView)findViewById(R.id.svPreview);
sHolder = svPreview.getHolder();
sHolder.addCallback(new SurfaceCallback());
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

//按下實體相機按鈕拍照
public boolean onKeyDown(int keyCode, KeyEvent event) {
//如果按下相機鈕或操控板中央鈕
if (keyCode == KeyEvent.KEYCODE_CAMERA ||
keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {

if(isPreview)
camera.autoFocus(new AutoFocusCallback());
return true;
}
return false;
}

//觸碰拍照
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction()== MotionEvent.ACTION_DOWN)
{
camera.takePicture(null,null, jpeg);

}
return super.onTouchEvent(event);
}


private void startPreview() {
if(camera != null && !isPreview){
camera.startPreview();
isPreview = true;
image = null;
System.gc();
}
}

private void stopPreview(){
if(camera != null && isPreview){
camera.stopPreview();
isPreview = false;
}
}

private void saveImage(File imageFile){
try {
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(imageFile));

image.compress(Bitmap.CompressFormat.JPEG, 90, bos);
bos.flush();
bos.close();
Toast.makeText(this,
getString(R.string.filePath) + imageFile.toString(),
Toast.LENGTH_LONG).show();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

private boolean isSDExist() {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
return true;
}
else {Toast.makeText(this,
R.string.SDCardNotFound, Toast.LENGTH_LONG).show();
return false;
}
}
[/sea]