多媒体 camera 拍照

时间:2021-12-05 23:31:17
多媒体 camera 拍照 Android应用提供Camera来控制拍照,按如下步骤: 1. 调用Camera的open()方法打开相机 2.调用Camera的getParameter()方法来获取拍照参数。该方法返回一个Camera.Parameter对象 3.调用Camera的SetParameter(),并将Camera.Parameter对象作为参数传入,这样可以控制相机的参数。 4.调用Camera的startPreview()方法开始预览取景,在预览取景之前需要调用Camera的setPreviewDisplay(surfaceHolder holder)防范设置使用哪个SurfaceView来显示取景图片 5.调用Camera的takePicture()方法进行拍照 6.结束程序时,调用Camera的stopPreview()来结束取景预览,并调用release释放资源
package com.example.captureimage;
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException;
import android.support.v7.app.ActionBarActivity; import android.support.v7.app.AlertDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.hardware.Camera.PictureCallback; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.Display; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.SurfaceHolder; import android.view.SurfaceHolder.Callback; import android.view.SurfaceView; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.EditText; import android.widget.ImageView;
public class MainActivity extends ActionBarActivity { SurfaceView sView; SurfaceHolder surfaceHolder; int screenWidth,screenHeight; Camera camera; boolean isPreview = false ; @Override protected void onCreate(Bundle savedInstanceState) { requestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE); Display display = wm.getDefaultDisplay(); //get height and weight screenHeight = display.getHeight(); screenWidth = display.getWidth(); System.out.print("wanlihua debug screen height and width :" + screenHeight + ","+screenWidth); //get surfaceView and surfaceHolder sView = (SurfaceView)findViewById(R.id.surfaceView1); surfaceHolder = sView.getHolder(); surfaceHolder.addCallback(new Callback() { @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub initCamera(); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub /*if(camera != null){ if(isPreview){ camera.stopPreview(); } camera.release(); camera = null; }*/ } }); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); }
private void initCamera() { if(!isPreview) camera = Camera.open(); if(camera != null && !isPreview){ try { Camera.Parameters parameters = camera.getParameters(); parameters.setPreviewSize(screenHeight,screenWidth); parameters.setPreviewFrameRate(4); parameters.setPictureFormat(PixelFormat.JPEG); parameters.set("jpeg-quality",85); parameters.setPictureSize(screenHeight,screenWidth); parameters.setRotation(90);
camera.setParameters(parameters); camera.setPreviewDisplay(surfaceHolder); camera.setDisplayOrientation(90); //setCameraDisplayOrientation(); camera.startPreview(); camera.autoFocus(null); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } isPreview = true; } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch (keyCode) { case KeyEvent.KEYCODE_CAMERA: case KeyEvent.KEYCODE_VOLUME_UP: case KeyEvent.KEYCODE_VOLUME_DOWN: if(camera != null && event.getRepeatCount() == 0){ camera.takePicture(null, null, myjpegCallback); return true; } break;
default: break; } return super.onKeyDown(keyCode, event); }
PictureCallback myjpegCallback = new PictureCallback() { @Override public void onPictureTaken(byte[] data, Camera camera) { // TODO Auto-generated method stub //根据拍照所得数据创建位图 final Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length); //加载、layout/save.xml文件对应布局资源 View saveDiag = getLayoutInflater().inflate(R.layout.save, null); final EditText photoName = (EditText)saveDiag.findViewById(R.id.photo_name); //get dialog imageview comp ImageView show = (ImageView)saveDiag.findViewById(R.id.show); show.setImageBitmap(bmp); new AlertDialog.Builder(MainActivity.this).setView(saveDiag).setPositiveButton("store", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub //create sdcard file File file = new File(Environment.getExternalStorageDirectory(), photoName.getText().toString()+".jpg"); file = new File(Environment.getExternalStorageDirectory()+"/DCIM/Camera/" + photoName.getText().toString()+".jpg" ); Log.d("wanlihua", "wanlihua debug File : "+ file); try { FileOutputStream outstream = new FileOutputStream(file); Log.d("wanlihua", "wanlihua debug outstream : "+ outstream); bmp.compress(CompressFormat.JPEG, 100, outstream); outstream.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).setNegativeButton("cancel", null).show(); camera.stopPreview(); camera.startPreview(); isPreview = true ; } }; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; }
@Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }