«

android照相机简单实例

时间:2024-3-2 17:01     作者:韩俊     分类: Android




package com.example.mycamera;





import java.io.File;

import java.io.FileOutputStream;





import android.app.Activity;

import android.content.pm.ActivityInfo;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.hardware.Camera;

import android.hardware.Camera.PictureCallback;

import android.os.Bundle;

import android.os.Environment;

import android.os.Handler;

import android.os.Message;

import android.view.MotionEvent;

import android.view.SurfaceHolder;

import android.view.SurfaceHolder.Callback;

import android.view.SurfaceView;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.ViewGroup.LayoutParams;

import android.view.Window;

import android.view.WindowManager;

import android.widget.Button;

import android.widget.ImageView;





public class MainActivity extends Activity {

private Button button;

private SurfaceView surfaceView;

private SurfaceHolder holder;

private Camera camera;

private ImageView imageView;

private Bitmap bitmap;





private Handler handler = new Handler() {





@Override

public void handleMessage(Message msg) {



//更新左下角上次照片预览小窗口

if (msg.what == 1) {

LayoutParams params = imageView.getLayoutParams();

params.height = 100;

params.width = 150;

imageView.setLayoutParams(params);

imageView.setImageBitmap(bitmap);





}

}

};





@Override

protected void onResume() {

if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

}

super.onResume();

}





@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);// 隐藏标题

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

WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏

setContentView(R.layout.activity_main);

button = (Button) this.findViewById(R.id.nagetivebutton);

button.setOnClickListener(new Mybuttonclicklistener());

imageView = (ImageView) this.findViewById(R.id.imageView1);

surfaceView = (SurfaceView) this.findViewById(R.id.surfaceview);





holder = surfaceView.getHolder();

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

holder.setFixedSize(176, 144);

holder.setKeepScreenOn(true);

holder.addCallback(new Myholdercallback());//添加回调响应

}





private class Myholdercallback implements Callback {





@Override

public void surfaceCreated(SurfaceHolder holder) {

try {

camera = Camera.open();

Camera.Parameters parameters = camera.getParameters();

// parameters.setPreviewSize(960, 480);

// parameters.setPreviewFrameRate(5);

// parameters.setPictureSize(1366, 768);

parameters.setJpegQuality(80);

camera.setParameters(parameters);

camera.setPreviewDisplay(holder);//相机预览显示在surfaceview控件上

camera.startPreview();





} catch (Exception e) {

e.printStackTrace();

}

}





@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

}





@Override

public void surfaceDestroyed(SurfaceHolder holder) {

if (camera != null) {

camera.release();

camera = null;

}

}

}





private final class PictureCallbackofme implements PictureCallback {





@Override

public void onPictureTaken(byte[] data, Camera camera) {

bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

Message message = Message.obtain();

message.what = 1;

handler.sendMessage(message);

File file = new File(Environment.getExternalStorageDirectory(),

System.currentTimeMillis() + ".jpg");





try {

FileOutputStream outputStream = new FileOutputStream(file);

outputStream.write(data);





outputStream.close();

camera.startPreview();

} catch (Exception e) {





e.printStackTrace();

}





}





}





private class Mybuttonclicklistener implements OnClickListener {





@Override

public void onClick(View v) {

camera.takePicture(null, null, new PictureCallbackofme());

}





}



//点击屏幕自动聚焦

@Override

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

camera.autoFocus(null);

}

return super.onTouchEvent(event);

}





}

标签: android

热门推荐