分类: Java
2011-11-30 10:53:49
List psizelist =
parameters.getSupportedPictureSizes(); if (null != psizelist && 0 < psizelist.size()) { int heights[] = new int[psizelist.size()]; Map for (int i = 0; i < psizelist.size(); i++) { Size size = (Size) psizelist.get(i); int sizehieght = size.height; int sizewidth = size.width; heights[i] = sizehieght; map.put(sizehieght, sizewidth); } Arrays.sort(heights);// 取最小尺寸 parameters.setPictureSize(map.get(heights[0]), heights[0]); } |
package com.exampls.camtest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
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.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
public class CamTestActivity extends Activity {
private Camera camera;
private SurfaceView surfaceView;
private boolean preview = false ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 窗口标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// 全屏
Window window = getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.main);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(320, 240); //设置分辨率
surfaceView.getHolder().addCallback(new SurfaceViewCallback());
}
private final class SurfaceViewCallback implements SurfaceHolder.Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
Camera.Parameters parameters = camera.getParameters();
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); // 获取当前屏幕管理器对象
Display display = wm.getDefaultDisplay(); // 获取屏幕信息的描述类
parameters.setPreviewSize(display.getWidth(), display.getHeight()); // 设置
parameters.setPreviewFrameRate(5);
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.set("jpeg-quality", 100);
List
if (null != pszize && 0 < pszize.size()) {
int height[] = new int[pszize.size()];// 声明一个数组
Map
for (int i = 0; i < pszize.size(); i++) {
Camera.Size size = (Camera.Size) pszize.get(i);
int sizeheight = size.height;
int sizewidth = size.width;
height[i] = sizeheight;
map.put(sizeheight, sizewidth);
}
Arrays.sort(height);
parameters.setPictureSize(map.get(height[0]), height[0]);
}else {
parameters.setPictureSize(display.getWidth(), display.getHeight());
}
camera.setParameters(parameters);
try {
camera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
camera.startPreview();
preview = true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if(camera != null) {
if(preview) {
camera.stopPreview();
preview = false;
}
camera.release();
camera = null;
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(camera != null && event.getRepeatCount() == 0 ) {
switch (keyCode) {
case KeyEvent.KEYCODE_SEARCH: // 搜索键
camera.autoFocus(null);
break;
case KeyEvent.KEYCODE_CAMERA: // 拍照键
case KeyEvent.KEYCODE_DPAD_CENTER: // 中间确认键
camera.takePicture(null, null, new TakePictureCallback());
// camera.startPreview();必须写在onPictureTaken 方法内部,因为 takePicture 内部是另开了一条线程异步的完成保存照片等操作。
// 虽然takePicture 方法完成了,但是并不代表其内部的工作全部完成,也不代表摄像头以及从上一次“拍照”任务中工作完毕
break;
default:
break;
}
return true;
}
return super.onKeyDown(keyCode, event);
}
private final class TakePictureCallback implements Camera.PictureCallback {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
// File file = new File(Environment.getExternalStorageState(), System.currentTimeMillis() + ".jpg");
File file = new File(Environment.getExternalStorageDirectory(), "cam.jpg");
try {
FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.close();
camera.stopPreview();
camera.startPreview();
preview = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
main.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
Androidmanifest.xml
package="com.exampls.camtest"
android:versionCode="1"
android:versionName="1.0">
android:label="@string/app_name">