Android自定义相机方法及保存照片方式


自定义相机步骤

1.创建相机

2.创建SurfaceView

3.关联相机和SurfaceView

4.调整相机的显示效果

5.自定义相机预览界面

CustomActivity:
public class CustomActivity extends AppCompatActivity implements SurfaceHolder.Callback {


    private Camera mCamera;
    private SurfaceView mPriview;
    private SurfaceHolder mHolder;
    private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            //data完成拍完之后照片的数据
            File tempFile = new File ("/sdcard/temp.png");
            try {
                FileOutputStream fos =  new FileOutputStream(tempFile);
                try {
                    fos.write(data);
                    fos.close();
                    Intent  intent  = new   Intent (CustomActivity.this,ResultActivity.class);
                    intent.putExtra("picPath",tempFile.getAbsolutePath());
                    startActivity(intent);
                    CustomActivity.this.finish();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

        }
    };
    @Bind(R.id.take_photo)
    Button takePhoto;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom);
        ButterKnife.bind(this);
        mPriview = (SurfaceView) findViewById(R.id.preview);
        mHolder = mPriview.getHolder();
        mHolder.addCallback(this);
        takePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Camera.Parameters parameters = mCamera.getParameters();
                parameters.setPictureFormat(ImageFormat.JPEG);
                parameters.setPreviewSize(400, 240);
                parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
                mCamera.autoFocus(new Camera.AutoFocusCallback() {
                    @Override
                    public void onAutoFocus(boolean success, Camera camera) {
                        //success代表对焦是否准确
                        if (success) {
                            mCamera.takePicture(null, null, mPictureCallback);
                        }
                    }
                });
            }
        });
        //设置相机自动对焦
        mPriview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCamera.autoFocus(null);
            }
        });

    }

    /**
     * 获取Camerad对象
     *
     * @return
     */
    private Camera getCamera() {
        Camera camera;
        try {
            camera = Camera.open();
        } catch (Exception e) {
            camera = null;
            e.printStackTrace();
        }
        return camera;

    }

    @Override
    protected void onPause() {
        super.onPause();
        releaseCamera();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mCamera == null) {
            mCamera = getCamera();
            if (mHolder != null) {
                setStartPreview(mCamera, mHolder);

            }
        }
    }

    /**
     * 开始预览相机内容
     */
    private void setStartPreview(Camera camera, SurfaceHolder holder) {
        try {
            //将holder对象传递到Camera对象中,完成绑定操作
            camera.setPreviewDisplay(holder);
            //将Camera预览角度进行调整90°
            camera.setDisplayOrientation(90);
            //开始在surface预览操作,但是是横屏的,在预览之前增加一个setDisplayOrientation方法
            camera.startPreview();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 释放相机占用的资源,请求了Camera但是没有释放会出现错误,所以建议跟系统生命周期绑定
     */
    private void releaseCamera() {
        if (mCamera != null) {
            //将相机的回调置空,取消mCamera跟surfaceView的关联操作
            mCamera.setPreviewCallback(null);
            //取消掉相机取景功能
            mCamera.stopPreview();
            mCamera.release();
            mCamera = null;
        }


    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        //开始camera与surfaceview的绑定
        setStartPreview(mCamera, holder);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        //重启整个功能,首先stopCamera,将相机进行关闭
        mCamera.stopPreview();
        setStartPreview(mCamera, holder);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseCamera();
    }
}

ResultActivity:
public class ResultActivity extends AppCompatActivity {

    @Bind(R.id.pic)
    ImageView pic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_result);
        ButterKnife.bind(this);
        String path = getIntent().getStringExtra("picPath");
        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);
            pic.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //        Bitmap bitmap = BitmapFactory.decodeFile(path);
        //        pic.setImageBitmap(bitmap);

    }
}
activity_custom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
 >

    <Button
        android:text="调用自定义相机拍照"
        android:id="@+id/take_photo"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <SurfaceView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/preview"/>
        <TextView
            android:layout_width="wrap_content"
            android:text="Android Studio CameraTest"
            android:textColor="#44FF0000"
            android:textSize="20sp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="126dp" />
    </RelativeLayout>

</LinearLayout>
activity_result.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >
<ImageView
    android:layout_width="match_parent"
    android:scaleType="fitCenter"
    android:layout_height="match_parent"
    android:id="@+id/pic"/>
</RelativeLayout>

需要添加的权限:
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.CAMERA" />

启发视频地址:http://www.imooc.com/learn/543

源码下载地址: http://download.csdn.net/detail/u010111008/9309597


智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告