package com.example.camera;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class CameraTest extends Activity implements SurfaceHolder.Callback {
private TextView textView01;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Button button01,button02,button03;
private boolean bIfPreview =false;
private Camera camera =null;
private String TAG = "CAMERA";
private String strCaptureFilePath ="/sdcard/camera_snap.jpg";
private LocationManager lm =null;
private String strLocationProvider ="";
private String str_location="";
private String longitude="";
private String latitude="";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//判断是否安装存储卡
if(!checkSDCard()){
mMakeTextToast(getResources().getText(R.string.str_node_err).toString(), true);
}
//取得屏幕解析像素
DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
textView01=(TextView)findViewById(R.id.myTextView01);
//以surfaceView作为相机Preview之用
surfaceView=(SurfaceView)findViewById(R.id.surface_camera);
//绑定surfaceView,取得surfaceHolder
surfaceHolder=surfaceView.getHolder();
//Activity必须实现surfaceHolder.callback
surfaceHolder.addCallback(CameraTest.this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
button01=(Button)findViewById(R.id.btnPreview);
button02=(Button)findViewById(R.id.btnBrowser);
button03=(Button)findViewById(R.id.btnTake);
//打开相机及Preview
button01.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
initCamera();
}});
button02.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
resetCamera();
// Intent intent=new Intent();
// intent.setClass(CameraTest.this, ShowTest.class);
// startActivity(intent);
// CameraTest.this.finish();
}});
button03.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
try{
//当有存储卡时,才允许拍照
if(checkSDCard()){
takePictures();
}else{
textView01.setText(getResources().getText(R.string.str_node_err).toString());
}
}catch(Exception ex){
Log.e(TAG,ex.getMessage());
}
}});
}
private void takePictures(){
if(camera!=null && bIfPreview){
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
}
private ShutterCallback shutterCallback=new ShutterCallback() {
@Override
public void onShutter() {
// TODO Auto-generated method stub
}
};
private PictureCallback rawCallback=new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
}
};
private PictureCallback jpegCallback =new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
//传入的第一个参数即为相片的byte
Bitmap bm=BitmapFactory.decodeByteArray(data, 0, data.length);
File sdcard_file=android.os.Environment.getExternalStorageDirectory();
String file_name=sdcard_file.getAbsolutePath();
String fileName="";
//加载经纬度信息
processLocationNetWork();
Location camera_location=getLocationProvider();
if(camera_location!=null){
processLocationUpdates(camera_location);
}
if(lm!=null){
lm.requestLocationUpdates(strLocationProvider, 2000, 10, locationListener);
}
if(str_location!=null && !"".equals(str_location)){
fileName +=str_location;
}
fileName+=System.currentTimeMillis();
//创建新文件
File myCaptureFile=new File(file_name);
Log.e(TAG, file_name);
try{
if(myCaptureFile.exists() && myCaptureFile.canWrite()){
File uadDir = new File(myCaptureFile.getAbsolutePath() + "/unlocking_android");
uadDir.mkdir();
if(uadDir.exists() && uadDir.canWrite()){
File obj_file = new File(myCaptureFile + "/" + fileName+".jpg");
obj_file.createNewFile();
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(obj_file));
//采用压缩文件的方法
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
//更新BufferedStream
bos.flush();
//结束OutputStream
bos.close();
Log.e(TAG, "success take picture");
//跳转页面
Intent jump_intent =new Intent();
jump_intent.setClass(CameraTest.this, ShowTest.class);
Bundle bundle =new Bundle();
bundle.putString("photo_name", obj_file.getAbsolutePath());
bundle.putString("longitude", longitude);
bundle.putString("latitude", latitude);
jump_intent.putExtras(bundle);
startActivity(jump_intent);
CameraTest.this.finish();
}
}else{
Log.e(TAG, "no purview");
}
}catch(Exception ex){
Log.e(TAG, ex.getMessage());
}
}
};
public final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
processLocationUpdates(location);
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
};
/**
* 初始化打开相机
*/
private void initCamera(){
if(!bIfPreview){
camera =Camera.open();
}
if(camera!=null && !bIfPreview){
Log.i(TAG, "inside the camera");
Camera.Parameters parameters=camera.getParameters();
//指定相片格式
parameters.setPictureFormat(PixelFormat.JPEG);
//指定Preview屏幕大小
parameters.setPreviewSize(320, 240);
//指定图片分辨率大小
parameters.setPictureSize(320, 240);
//将Camera.Parameters给Camera
camera.setParameters(parameters);
try{
camera.setPreviewDisplay(surfaceHolder);
}catch(Exception ex){
Log.e(TAG, ex.getMessage());
}
camera.startPrevie
- 1
- 2
- 3
- 4
- 5
前往页