首先,我本人以前沒接觸過Android又是剛工作一段時間,前段時間比較忙,今天來跟大家分享下最近的收穫吧,最近項目中需要一個拍照以及修剪圖片,並保存修剪之後的圖片到相對應的位置區,我們直接進入主題哈。不對的地方多多指教,勿噴。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
>
<ImageView
android:id="@+id/image"
android:layout_width="70dp"
android:layout_height="100dp"
android:src="@drawable/qzx"/>
</LinearLayout>
<Button
android:id="@+id/toCamera"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="拍照" />
<Button
android:id="@+id/toSelectPhoto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="相冊" />
<Button
android:id="@+id/uplodphoto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="上傳" />
</LinearLayout>
首先呢這是前臺的一個佈局,我相信大家都能看懂。我們通過按鈕拍照,就可以調用拍照的功能啦,
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_onphoto);//這裏呢是指向那個拍照的前臺界面 也就是上面的佈局
takephoto=(Button)findViewById(R.id.toCamera);//這裏是調用拍照那個button
selectphoto=(Button) findViewById(R.id.toSelectPhoto);//同理 選擇相冊
takephoto.setOnClickListener(listener);//listener 當然了這個是個監聽啦
selectphoto.setOnClickListener(listener);
upload.setOnClickListener(listener);
PortableSystemApplication gisQueryApplication = (PortableSystemApplication) this.getApplication();
gisQueryApplication.addActivity(this);
this.mHandler = new MyHandler(Looper.myLooper());
imageView=(ImageView)findViewById(R.id.image);
}
private OnClickListener listener=new OnClickListener(){
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.toCamera:
OnPhotoActivity.this.onPhoto();
break;
case R.id.toSelectPhoto:
pickPhoto();
break;
case R.id.uplodphoto:
if(!uploadphoto.equals(""))
{
upload.setEnabled(false);
onPreExecute();
UploadThread uploadThread = new UploadThread();
new Thread(uploadThread).start();
}else{
Toast.makeText(OnPhotoActivity.this, "請選擇圖片", Toast.LENGTH_SHORT).show();
return;
}
break;
}
}
};
下面使我們具體調用拍照功能的代碼了;
protected void onPhoto() {
if (Environment.getExternalStorageState().equals("mounted")) { //判斷sd卡是否存在
String str1 =((PortableSystemApplication) this.getApplication()).getProjectPath()+ "CameraPhoto/";
String str2 = UtilsTools.GetCurrentTime() + ".jpg";
File localFile = new File(str1);
if (!(localFile.exists()))
localFile.mkdirs();
m_CurrentCamaraPath = str1 + str2;
m_CurrentCamaraPhotoName = str2;
Log.i("拍照1", "路徑=" + m_CurrentCamaraPath);
Log.i("拍照1", "名字=" + m_CurrentCamaraPhotoName);
Uri localUri = Uri.fromFile(new File(str1, str2));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
str1, str2)));
startActivityForResult(intent, PHOTO_GRAPH);
Log.i("OnPhotoActivity.this.m_CurrentCamaraPath", "=========="+m_CurrentCamaraPath);
} else {
Toast.makeText(this, "無SD卡", 1).show();
}
}
這部分代碼的實現從相冊選擇;
protected void pickPhoto() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, PHOTO_ZOOM);
}
這部分是實現裁剪的;
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, IMAGE_UNSPECIFIED);
intent.putExtra("crop", "true");
// aspectX aspectY 是寬高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX outputY 是裁剪圖片寬高
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 500);
intent.putExtra("return-data", true);
OnPhotoActivity.this.startActivityForResult(intent, PHOTO_RESOULT);
}
下面是調用拍照返回來的一些參數
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == PHOTO_GRAPH) {
// 設置文件保存路徑
if(!m_CurrentCamaraPath.equals("")){
File picture = new File(m_CurrentCamaraPath);
startPhotoZoom(Uri.fromFile(picture));
}else{
Toast.makeText(this, "m_CurrentCamaraPath 爲“”", 1).show();
}
}
if(data == null)
{
return;
}
if(requestCode == PHOTO_ZOOM ) //從相冊取圖片,有些手機有異常情況,請注意
{
startPhotoZoom(data.getData());
}
// 處理結果
if (requestCode == PHOTO_RESOULT) {
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");
imageView.setImageBitmap(bitmap); //把圖片顯示在ImageView控件上
SavePicInLocal( bitmap);
}
try {
// 將臨時文件刪除
picture.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
super.onActivityResult(requestCode, resultCode, data);
}
這個是將裁剪返回的數據保存
private void SavePicInLocal(Bitmap bitmap) {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ByteArrayOutputStream baos = null; // 字節數組輸出流
try {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] byteArray = baos.toByteArray();// 字節數組輸出流轉換成字節數組
String patch =((PortableSystemApplication) this.getApplication()).getProjectPath()+ "CameraPhoto/";
String picName = UtilsTools.GetCurrentTime() + ".jpg";
// String picName =m_CurrentCamaraPhotoName;
uploadphoto=patch+picName;
File file = new File(patch, picName);
// 將字節數組寫入到剛創建的圖片文件中
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(byteArray);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (baos != null) {
try {
baos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}