zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android Camera开发系列(下)——自定义Camera实现拍照查看图片等功能

Android开发 实现 系列 功能 查看 图片 自定义
2023-09-14 08:59:39 时间
 ?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:orientation="vertical" 

 SurfaceView

 android:id="@+id/sv"

 android:layout_width="match_parent"

 android:layout_height="0dp"

 android:layout_weight="1" / 

 Button

 android:id="@+id/btn_camera"

 android:layout_width="match_parent"

 android:layout_height="wrap_content"

 android:text="拍照" / 

 /LinearLayout 

这里因为是模拟器,他只有前置摄像头,本应该旋转270°的,我们只要知道通过以上的方法可以同步预览影像就可以了


btn_camera = (Button) findViewById(R.id.btn_camera);

 btn_camera.setOnClickListener(new OnClickListener() {

 @Override

 public void onClick(View v) {

 // 获取当前相机参数

 Camera.Parameters parameters = mCamera.getParameters();

 // 设置相片格式

 parameters.setPictureFormat(ImageFormat.JPEG);

 // 设置预览大小

 parameters.setPreviewSize(800, 480);

 // 设置对焦方式,这里设置自动对焦

 parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);

 mCamera.autoFocus(new AutoFocusCallback() {

 @Override

 public void onAutoFocus(boolean success, Camera camera) {

 // 判断是否对焦成功

 if (success) {

 // 拍照 第三个参数为拍照回调

 mCamera.takePicture(null, null, pc);

 });

 ?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:orientation="vertical" 

 TextView

 android:id="@+id/tv_path"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content" / 

 ImageView

 android:id="@+id/iv_photo"

 android:layout_width="wrap_content"

 android:layout_height="wrap_content" / 

 /LinearLayout 

protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_photo); String path = getIntent().getStringExtra("path"); tv_path = (TextView) findViewById(R.id.tv_path); // 显示路径 tv_path.setText(path); iv_photo = (ImageView) findViewById(R.id.iv_photo); // 调整角度 try { FileInputStream fis = new FileInputStream(path); Bitmap bitmap = BitmapFactory.decodeStream(fis); // 矩阵 Matrix matrix = new Matrix(); matrix.setRotate(90); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); iv_photo.setImageBitmap(bitmap); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace();

但是相机是一个大坑,各种不兼容,而且还有前后置摄像头之分哟,上面代码病没有提及前置,前置需要翻转270°,并且我们之前就应该判断是否是前置,再进行旋转,这个只是初学的示例代码,有兴趣的可以下载demo玩玩


“千变万化”——神奇的Android图片规格调整器(功能梳理篇) 由于放假的缘故,还是没时间直接上手APP,所以趁着晚上有时间,不妨为“千变万化”APP梳理一下功能。这样也能为我构思该APP提供更好的设计方向,防止出现想到一处写一处的混乱情况。