/******************************************************************************
* Copyright (C) 2012 by Mianyang Netop Telecom Equipment Co.,Ltd
*
* Project name: camera
* File Name: camera.java
* Author: ChenJianHong
* Purpose: The Main Page Of camera
* Update Date: 2012.11.19 this file is created by ChenJh
*******************************************************************************/
package com.camera;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;
import com.camera.ColorConverter;
import com.camera.ImageProcessing;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
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.text.format.Time;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class camera extends Activity
{
private static final String TAG = "camera";
private SurfaceView surfaceView;
private Camera camera;
private boolean preview;
private ImageButton exit;
// private ImageButton takephoto;
private Button test1;
private Button test2;
private Button test3;
private Button test4;
private ColorConverter ColorConverter;
private ImageProcessing ImageProcessing;
private int effect = 0;
private int width, height;
private Bitmap bm;
private Timer mTimer = null;
private boolean EnableDecodeBitmap = true;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Window window = getWindow();
requestWindowFeature(Window.FEATURE_NO_TITLE);// 没有标题
window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);// 高亮
setContentView(R.layout.main);
ButtonClickingListener buttonlistener = new ButtonClickingListener();
surfaceView = (SurfaceView) this.findViewById(R.id.camera_surface);
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
width = 400;
height = 240;
exit = (ImageButton) findViewById(R.id.exit);// 退出
exit.setOnClickListener(buttonlistener);
// takephoto = (ImageButton) findViewById(R.id.takephoto);// 拍照
// takephoto.setOnClickListener(buttonlistener);
test1 = (Button) findViewById(R.id.test1);// 特效1
test1.setOnClickListener(buttonlistener);
test2 = (Button) findViewById(R.id.test2);// 特效2
test2.setOnClickListener(buttonlistener);
test3 = (Button) findViewById(R.id.test3);// 特效2
test3.setOnClickListener(buttonlistener);
test4 = (Button) findViewById(R.id.test4);// 恢复
test4.setOnClickListener(buttonlistener);
surfaceView.getHolder().setFixedSize(width, height); // 设置分辨率
/* 下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前 */
surfaceView.getHolder().addCallback(new SurfaceCallback());
}
// 按钮监听
private final class ButtonClickingListener implements View.OnClickListener
{
public void onClick(View v)
{
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
{
Toast.makeText(camera.this, R.string.sdcarderror, 1).show();
return;
}
try
{
switch (v.getId())
{
case R.id.exit:
new AlertDialog.Builder(camera.this).setTitle("提示").setMessage(
"确定退出照相机?").setPositiveButton("确定",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
Intent exit = new Intent(Intent.ACTION_MAIN);
exit.addCategory(Intent.CATEGORY_HOME);
exit.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(exit);
System.exit(0);
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
// 取消按钮事件
dialog.cancel();
}
}).show();
break;
case R.id.test1:
effect = 1;
break;
case R.id.test2:
effect = 2;
break;
case R.id.test3:
effect = 3;
break;
case R.id.test4:
effect = 0;
break;
// case R.id.takephoto:
// camera.takePicture(null, null, new TakePictureCallback());
// break;
}
} catch (Exception e)
{
Toast.makeText(camera.this, R.string.error, 1).show();
Log.e(TAG, e.toString());
}
}
}
@Override
protected void onDestroy()
{
// TODO Auto-generated method stub
if (camera != null)
{
camera.setPreviewCallback(null);
camera.stopPreview();
camera.release();
camera = null;
}
super.onDestroy();
}
private final class SurfaceCallback implements SurfaceHolder.Callback
{
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height)
{
}
public void surfaceCreated(SurfaceHolder holder)
{
if (camera == null)
{
camera = Camera.open();// 打开相机
} else
{
Toast.makeText(camera.this, "相机正在使用中", 1).show();
}
WindowManager wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Camera.Parameters parameters = camera.getParameters();
parameters
.setPreviewSize(400, 240);// 设置预览照片的大小
parameters.setPreviewFrameRate(24);// 每秒10帧
parameters.setPictureFormat(PixelFormat.JPEG);// 设置照片的输出格式
parameters.set("jpeg-quality", 100);// 照片质量
parameters
.setPictureSize(display.getWidth() / 4, display.getHeight() / 4);// 设置照片的大小
camera.setParameters(parameters);
camera.setPreviewCallback(new PreviewCallBack());// 通过SurfaceView显示取景画面
camera.startPreview();// 开始预览
preview = true;
}
public void surfaceDestroyed(SurfaceHolder holder)
{
if (camera != null)
{
if (preview)
camera.stopPreview();
camera.release();
}
}
}
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (camera != null && event.getRepeatCo
- 1
- 2
- 3
- 4
- 5
前往页