本文主要是介绍Android学习之调用系统相机拍照、截图并保存,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近写一款拍照的APP用到了拍照,截图,并返回ImageView。网上找了好几种fang法,发现最好还是调用系统自带的,既简单又方便。
启用android 系统相机
intent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
启用android截图
intent = new Intent("com.android.camera.action.CROP");//动作-裁剪
下面是我的代码
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Environment;
import android.provider.MediaStore;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.net.Uri;import android.widget.TextView;
import android.widget.Toast;
import com.soundcloud.android.crop.Crop;import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;import me.qincai.newton.R;
import me.qincai.newton.listener.NavigationFinishClickListener;
import me.qincai.newton.util.Utils;public class MainActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener {private static final int REQUEST_IMAGE_CAPTURE = 330;private static File photoFile;private static File cropFile;public static String crop;public static String crop1;@Bind(R.id.main_toolbar)protected Toolbar toolbar;@Bind(R.id.tv_register)protected TextView tv_register;@Bind(R.id.btn_capture)protected com.rey.material.widget.Button btn_capture;private static final int REQUEST_CROP = 332;public static final int REQUEST_LOGIN = 331;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ButterKnife.bind(this);toolbar.setNavigationOnClickListener(new NavigationFinishClickListener(this));toolbar.inflateMenu(R.menu.menu_main);toolbar.setOnMenuItemClickListener(this);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.menu_main, menu);return true;}@Overridepublic boolean onMenuItemClick(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();switch (id) {case R.id.action_user:if (Utils.isLogin()) {Intent intent = new Intent(MainActivity.this, ProfileActivity.class);startActivity(intent);} else {Utils.loginRequired(MainActivity.this);}return true;case R.id.action_questions:Intent intent = new Intent(MainActivity.this, QuestionsActivity.class);startActivity(intent);return true;default:return false;}}@OnClick(R.id.btn_capture)public void onCapture() {// 未登陆则引导登陆if (!Utils.isLogin()) {Utils.loginRequired(MainActivity.this);} else {Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);if (intent.resolveActivity(getPackageManager()) != null) {try {photoFile = createImageFile();} catch (IOException e) {Toast.makeText(this, "写入图片出错", Toast.LENGTH_SHORT).show();}if (photoFile != null) {intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);}}}}@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);if (data == null)if (resultCode != RESULT_OK)return;switch (requestCode) {case REQUEST_IMAGE_CAPTURE:Uri u = Uri.fromFile(photoFile);cropPhoto(u);break;case REQUEST_CROP:Log.e("截图成功onActivityResult", "data is " + data.toString());handleCrop();break;case REQUEST_LOGIN:break;}}public void cropPhoto(Uri uri) {try {cropFile = createImageFile();}catch (IOException e){Log.e("jianliwenjiancuowu","sedfwedf");}if (uri == null) {return;}Intent intent = new Intent("com.android.camera.action.CROP");//动作-裁剪intent.setDataAndType(uri, "image/*");//类型intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cropFile)); 输出文件intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());// 输出格式intent.putExtra("crop", true);intent.putExtra("scale", true);// 缩放intent.putExtra("scaleUpIfNeeded", true);// 如果小于要求输出大小,就放大intent.putExtra("return-data", false);// 不返回缩略图intent.putExtra("noFaceDetection", true);// 关闭人脸识别startActivityForResult(intent,REQUEST_CROP);}private void handleCrop() {Intent intent = new Intent(MainActivity.this, PreviewActivity.class);intent.putExtra("image_cropped_file", cropFile.toString());startActivity(intent);}private File createImageFile() throws IOException {// Create an image file nameString timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA).format(new Date());String imageFileName = "QC_" + timeStamp + "_";File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);File image = File.createTempFile(imageFileName, /* prefix */".jpg", /* suffix */storageDir /* directory */);// Save a file: path for use with ACTION_VIEW intentsreturn image;}@OnClick(R.id.tv_register)protected void onRegisterClick() {Intent intent = new Intent(MainActivity.this, RegisterActivity.class);startActivity(intent);}
}
这就是简单的调用系统相机,如果大家有兴趣想自定义一个相机的话,可以看一看我的这篇博客: 【Android 开发】SufaceView自定义相机拍照
这篇关于Android学习之调用系统相机拍照、截图并保存的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!