package com.jovian.android.ui;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.jovian.android.pqgl.R;
public class ShowCameraDialog extends Dialog implements SurfaceHolder.Callback {
private TextView textView01;
private SurfaceView surfaceView;
private SurfaceHolder surfaceHolder;
private Button PhotoingBtn, backBtn;
private boolean bIfPreview = false;
private Camera camera = null;
private String TAG = "CAMERA";
private String strCaptureFilePath = "/sdcard/camera_snap.jpg";
Context context;
private String shequ;
// 用来刷新EditSgyhDialog
EditSgyhDialog dialog;
public EditSgyhDialog getDialog() {
return dialog;
}
public void setDialog(EditSgyhDialog dialog) {
this.dialog = dialog;
}
public ShowCameraDialog(Context context, String shequ) {
super(context, R.style.dialog_fullscreen);
this.context = context;
this.setContentView(R.layout.show_camera);
// 判断是否安装存储卡
setupview();
this.shequ = shequ;
}
public ShowCameraDialog(Context context) {
super(context, R.style.dialog_fullscreen);
this.context = context;
this.setContentView(R.layout.show_camera);
// 判断是否安装存储卡
setupview();
}
private void setupview() {
if (!checkSDCard()) {
mMakeTextToast("未安装sd卡", true);
}
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(ShowCameraDialog.this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
PhotoingBtn = (Button) findViewById(R.id.btnTake);
backBtn = (Button) findViewById(R.id.btnCancel);
PhotoingBtn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
try {
// 当有存储卡时,才允许拍照
if (checkSDCard()) {
if (camera != null) {
camera.autoFocus(new AutoFocusCallback() {
@Override
public void onAutoFocus(boolean focused,
Camera camera) {
if (focused)
camera.takePicture(shutterCallback,
rawCallback, jpegCallback);
}
});
}
} else {
textView01.setText("未安装SD卡");
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
});
backBtn.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
if (null != dialog) {
dialog.dealPhoto();
}
ShowCameraDialog.this.cancel();
}
});
}
/**
* 初始化打开相机
*/
private void initCamera() {
if (camera != null) {
try {
Camera.Parameters parameters = camera.getParameters();
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
// parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
// parameters.setWhiteBalance(Parameters.WHITE_BALANCE_AUTO);
// parameters.setSceneMode(Parameters.SCENE_MODE_AUTO);
// parameters.setPreviewSize(1024, 768);
/*
* 设定相片大小为1024*768, 格式为JPG
*/
// parameters.setPreviewFormat(PixelFormat.JPEG);
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.setPictureSize(1024, 768);
camera.setParameters(parameters);
/* 打开预览画面 */
camera.startPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 检查SDCard是否存在
*
* @return
*/
private boolean checkSDCard() {
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
}
/**
* 提示信息
*
* @param str
* @param isLong
*/
private void mMakeTextToast(String str, boolean isLong) {
if (isLong) {
Toast.makeText(context, str, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
/**
* 删除文件
*
* @param strFileName
*/
private void delFile(String strFileName) {
try {
File myFile = new File(strFileName);
if (myFile.exists()) {
myFile.delete();
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
/* 停止相机的method */
private void stopCamera() {
if (camera != null) {
try {
/* 停止预览 */
camera.stopPreview();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private ShutterCallback shutterCallback = new ShutterCallback() {
@Override
public void onShutter() {
}
};
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) {
if (null == dialog) {
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
File sdcard_file = android.os.Environment
.getExternalStorageDirectory();
String file_name = sdcard_file.getAbsolutePath() + "/hy_ht_new";
String fileName = shequ;
File myCaptureFile = new File(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");
// 跳转页面
stopCamera();
camera.release();
camera = null;
ShowCameraDialog.this.cancel();
String photoPath = obj_file.getAbsolutePath();
ShowPhotoUploadDialog upload = new ShowPhotoUploadDialog(
context, photoPath);
upload.show();
}
} else {
Log.e(TAG, "no purview");
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
} else {
// 传入的第一个参数即为相片的byte
Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
dialog.getBitmaps().add(bm);
File sdcard_file = android.os.Environment
.getExternalStorageDirectory();
String file_name = sdcard_file.getAbsolutePath() + "";
String 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");
// 跳转页面
stopCamera();
camera.release();
camera = null;
String photoPath = obj_file.getAbsolutePath();
ShowCameraDialog.this.cancel();
ShowPhotoUploadDialog upload = new ShowPhotoUploadDialog(
context, photoPath);
upload.setmEditSgyhDialog(dialog);
upload.show();
}
} else {
Log.e(TAG, "no purview");
}
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
}
};
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
/* 相机初始化 */
initCamera();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
/* 打开相机, */
camera = Camera.open();
initCamera();
camera.setPreviewDisplay(holder);
} catch (IOException exception) {
camera.release();
camera = null;
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
stopCamera();
camera.release();
camera = null;
}
}
package com.jovian.android.ui;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.jovian.android.pqgl.MessageService;
import com.jovian.android.pqgl.PQGLMapActivity;
import com.jovian.android.pqgl.R;
import com.jovian.util.FileUtils;
import com.jovian.util.UploadUtil;
public class ShowPhotoUploadDialog extends Dialog {
Context context;
private EditText edit_text;
private Button button_preview;
private Button button_ok;
String photoPath = "";
// 用来向EditSgyhDialog传递数值
EditSgyhDialog mEditSgyhDialog;
public interface UpPhotoOverListener {
public void uploadOver(String result);
}
public EditSgyhDialog getmEditSgyhDialog() {
return mEditSgyhDialog;
}
public void setmEditSgyhDialog(EditSgyhDialog mEditSgyhDialog) {
this.mEditSgyhDialog = mEditSgyhDialog;
}
public ShowPhotoUploadDialog(final Context context, String photoPath) {
super(context);
this.context = context;
this.photoPath = photoPath;
setContentView(R.layout.show);
setupview();
}
public void setupview() {
this.setTitle("上传照片");
final String photoPath1 = photoPath;
ImageView image_view = (ImageView) findViewById(R.id.image_view01);
edit_text = (EditText) findViewById(R.id.edit_text01);
button_preview = (Button) findViewById(R.id.button_preview);
button_ok = (Button) findViewById(R.id.button_ok);
Bitmap bm = BitmapFactory.decodeFile(photoPath);
image_view.setImageBitmap(bm);
button_preview.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
show_exit();
}
});
button_ok.setOnClickListener(new Button.OnClickListener() {
UpLoadAndSave loadAndSave = null;
@Override
public void onClick(View v) {
// 正式地址,到时候需要修改为该地址
loadAndSave = new UpLoadAndSave(photoPath1,
PQGLMapActivity.restserviceURL + "fileupload");
loadAndSave.execute();
}
});
}
private void show_exit() {
ShowPhotoUploadDialog.this.dismiss();
ShowCameraDialog camera = new ShowCameraDialog(context);
if (null != mEditSgyhDialog) {
camera.setDialog(mEditSgyhDialog);
}
camera.show();
}
class UpLoadAndSave extends AsyncTask<String, Integer, String> {
String filePath;
String url;
public UpLoadAndSave(String filePath, String url) {
this.filePath = filePath;
this.url = url;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String request = null;
File file = new File(photoPath);
// 将拍到的照片保存到本地
// try {
// String name = file.getName();
// FileInputStream fileInput = new FileInputStream(file);
// FileUtils utils = new FileUtils();
// utils.write2SDFromInput(ToLocalPath, name, fileInput);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
// 上传到服务器,并且将返回的照片ID传递给EditSgyhDialog
if (file != null) {
request = UploadUtil.uploadFile(file, url);
if (request != null && null != mEditSgyhDialog) {
mEditSgyhDialog.getList().add(request);
}
}
return request;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// 当回到编辑页面进行刷新EditSgyhDialog
if (null != mEditSgyhDialog) {
mEditSgyhDialog.dealPhoto();
} else {
Bundle bundle = new Bundle();
bundle.putString("zpid", result);
Intent intent = new Intent();
intent.setAction("SAVE_ZPID");
intent.putExtras(bundle);
context.sendBroadcast(intent);
}
ShowPhotoUploadDialog.this.cancel();
}
}
}