package com.scan;
import java.io.IOException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import com.scan.adapter.FrameAdapter;
import com.scan.listener.CameraframeListener;
public class TakePhotoActivity extends Activity implements
SurfaceHolder.Callback, OnClickListener {
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Camera mCamera;
/**
* 调节焦距的大小
*/
private ImageView scaleButton;
private ImageView smallButton;
private boolean mPreviewRunning;
private ImageView mImageView;
private Button frameButton;
private Button switchButton;
private FrameAdapter frameAdapter;
private LinearLayout camerabg;
public void makeHasColor(View v,int id)
{
v.setBackgroundResource(id);
}
public void hideColor(View v,int id)
{
v.setBackgroundResource(id);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.takephoto);
initViews();
}
public void initViews()
{
camerabg=(LinearLayout)findViewById(R.id.camerabottom);
mSurfaceView = (SurfaceView) findViewById(R.id.surfaceview);
mImageView = (ImageView) findViewById(R.id.image);
scaleButton=(ImageView)findViewById(R.id.scalebig);
switchButton=(Button)findViewById(R.id.switchbutton);
switchButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
hideColor(frameButton,R.drawable.frame);
hideColor(smallButton,R.drawable.camera_scalesmall);
makeHasColor(switchButton,R.drawable.camera_switch_down);
hideColor(scaleButton, R.drawable.camera_scalebig);
Intent i = new Intent(TakePhotoActivity.this,MainActivity.class);
startActivity(i);
finish();
}});
scaleButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
makeHasColor(scaleButton, R.drawable.camera_scalebig_down);
hideColor(frameButton,R.drawable.frame);
hideColor(smallButton,R.drawable.camera_scalesmall);
hideColor(switchButton,R.drawable.camera_switch);
Parameters params = mCamera.getParameters();
params.setPictureFormat(PixelFormat.JPEG);// 设置图片格式
int a=params.getZoom();
System.out.println("zboolea--->"+params.isZoomSupported());
System.out.println("zomm----->"+a);
//params.setZoom(10);
params.setPreviewSize(800, 400);
mCamera.setParameters(params);
}});
smallButton=(ImageView)findViewById(R.id.small);
smallButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
hideColor(frameButton,R.drawable.frame);
makeHasColor(smallButton,R.drawable.camera_scalesmall_down);
hideColor(switchButton,R.drawable.camera_switch);
hideColor(scaleButton, R.drawable.camera_scalebig);
}});
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mSurfaceView.setOnClickListener(this);
frameButton=(Button)findViewById(R.id.frame);
frameButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
makeHasColor(frameButton,R.drawable.frame_down);
hideColor(smallButton,R.drawable.camera_scalesmall);
hideColor(switchButton,R.drawable.camera_switch);
hideColor(scaleButton, R.drawable.camera_scalebig);
showFrameDialog();
}});
}
@Override
public void onClick(View v) {
mCamera.takePicture(null, null, mPictureCallback);
}
/**
* 2.3 的调用方法
* 2.2 的怎么调用呢.
*/
@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera =Camera.open();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// TODO Auto-generated method stub
//mCamera = Camera.open();
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
// for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
// Camera.CameraInfo info = new Camera.CameraInfo();
// Camera.getCameraInfo(i, info);
// if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {//这就是前置摄像头,亲。
// mCamera = Camera.open(i);
// }
// }
// }
// if (mCamera == null) {
// mCamera = Camera.open();
// }
// try {
// mCamera.setPreviewDisplay(holder);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
System.out.println("witdht---->"+width);
System.out.println("height---->"+height);
if (mPreviewRunning) {
mCamera.stopPreview();
}
mPreviewRunning = true;
Parameters params = mCamera.getParameters();
//params.set("camera-id", 2);
//params.set("video_input","secondary");
params.setPictureFormat(PixelFormat.JPEG);// 设置图片格式
params.setPreviewSize(width, height);
// params.set("rotation", 180);
// params.set("orientation", "portrait");
mCamera.setParameters(params);
System.out.println("Parametesr------>"+mCamera.getParameters().toString());
mCamera.startPreview();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
mCamera.setPreviewCallback(null) ;
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
mCamera = null;
}
/**
* 拍照的回调接口
*/
PictureCallback mPictureCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
if (data != null) {
System.out.println("data---------------.");
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);
/**
* 将拍到的照片绘制出来
*/
writeTosdcard();
// Canvas canvas =mSurfaceHolder.lockCanvas();
// canvas.drawBitmap(bitmap, 0, 0, null);
// mSurfaceHolder.unlockCanvasAndPost(canvas);
mImageView.setImageBitmap(bitmap);
mImageView.setVisibility(View.VISIBLE);
mSurfaceView.setVisibility(View.GONE);
if (mPreviewRunning) {
mCamera.stopPreview();
mPreviewRunning = false;
}
}
}
};
private PopupWindow framePopupWindow;
/**
* 显示frame dialog
* dialog 如果里面存着这listview ,gridview
* 必须要设定大小.
*/
public void showFrameDialog()
{
System.out.println("shwoFrameDialog---.");
View view = LayoutInflater.from(TakePhotoActivity.this).inflate(R.layout.camera_frame, null);
ListView listview =(ListView)view.findViewById(R.id.gridview);
frameAdapter= new FrameAdapter(TakePhotoActivity.this);
frameAdapter.setCurrentPosition(0);
listview.setAdapter(frameAdapter);
// listview.setOnItemClickListener(new ListView.OnItemClickListener(){
//
// @Override
// public void onItemClick(AdapterView<?> parent, View view,
// int position, long id) {
//
// System.out.println("onItem");
// frameAdapter.setCurrentPosition(position);
// frameAdapter.notifyDataSetChanged();
//
// }});
framePopupWindow = new PopupWindow(view);
framePopupWindow.setWidth(110);
framePopupWindow.setHeight(200);
framePopupWindow.setFocusable(true);
//framePopupWindow.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
framePopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.camera_popup_bg));
framePopupWindow.showAsDropDown(frameButton, -10, 0);
}
CameraframeListener listener = new CameraframeListener() {
@Override
public void changeColor(int position) {
switch (position) {
case 0:
camerabg.setBackgroundResource(R.drawable.camera_autumncolor);
break;
case 1:
camerabg.setBackgroundResource(R.drawable.camera_autumncolor);
break;
case 2:
camerabg.setBackgroundResource(R.drawable.camera_greencolor);
break;
case 3:
camerabg.setBackgroundResource(R.drawable.camera_fashioncolor);
break;
default:
break;
}
}
};
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if(framePopupWindow !=null &&framePopupWindow.isShowing())
{
framePopupWindow.dismiss();
}
}
public void writeTosdcard()
{
String str =Environment.getExternalStorageState();
}
// public int getFrontCameraId()
// {
//
// int tap= Build.VERSION.SDK_INT;
// if(tap>=9)
// {
// Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
// for(int i=0;i<Camera.getNumberOfCameras();i++)
// {
// Camera.getCameraInfo(i, cameraInfo);
// if(Camera.CameraInfo.CAMERA_FACING_FRONT==cameraInfo.facing)
// {
// return i;
// }
//
// }
// return 0;
//
// }
// else
// {
// return 0;
// }
// }
}