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
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
CameraTest.rar (40个子文件)
CameraTest
bin
classes.dex 24KB
com
example
config
Configuration.class 2KB
Configuration2.class 1KB
camera
CameraTest$7.class 2KB
ShowTest.class 8KB
R$layout.class 422B
ShowTest$1.class 777B
ShowTest$2$1.class 1014B
R$id.class 704B
R$drawable.class 400B
CameraTest$3.class 4KB
CameraTest$4.class 1KB
R$attr.class 340B
R.class 575B
CameraTest$2.class 776B
CameraTest$6.class 790B
CameraTest$5.class 789B
R$string.class 942B
ShowTest$2.class 2KB
CameraTest.class 12KB
CameraTest$1.class 669B
R$raw.class 392B
CameraTest.apk 27KB
resources.ap_ 14KB
res
drawable-ldpi
icon.png 2KB
raw
upload_prop.properties 42B
drawable-hdpi
icon.png 4KB
values
strings.xml 955B
drawable-mdpi
icon.png 3KB
layout
show.xml 1KB
main.xml 1KB
assets
default.properties 364B
gen
com
example
camera
R.java 2KB
src
com
example
config
Configuration.java 882B
Configuration2.java 788B
camera
ShowTest.java 8KB
CameraTest.java 13KB
.project 846B
.classpath 280B
AndroidManifest.xml 1KB
共 40 条
- 1
ElvaJiao
- 粉丝: 0
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页