文章标题android自制多媒体视频播放器和图片压缩技术(字节数组转成Bitmap)

1.在androidmanifest.xml配置


  <uses-permission android:name="android.permission.CAMERA" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO">uses-permission>

在xml中通过SurfaceView来显示

 <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sv_main_surface"
        />

Java代码的照相机实现过程,通过 Camera的一些参数,将取到的数值放画面展示到SurfaceView

 sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
        sv_main_surface.getHolder().addCallback(new SurfaceHolder.Callback() {


            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                //打开照相机
                camera = Camera.open();
                //设置参数
                Camera.Parameters parameters=camera.getParameters();
                parameters.setPictureFormat(PixelFormat.JPEG);
                parameters.set("jpeg-quality",85);
                camera.setParameters(parameters);

                //将画面展示到SurfaceView
                try {
                    camera.setPreviewDisplay(sv_main_surface.getHolder());
                    //开启预览
                    camera.startPreview();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                //关闭照相机
                if(camera!=null){
                    camera.release();
                    camera.stopPreview();
                    camera=null;
                }
            }
        });

//将图片大小进行压缩,图片压缩技术(字节数组转成Bitmap),
//通过文件输出流FileOutputStream的形式将图片进行压缩

 public void takePhoto(View view) {
        camera.takePicture(null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] bytes, Camera camera) {
                //实例化文件输出流
                //写

                //压缩(图片)
                //将字节数组转成Bitmap
                Bitmap bitmap=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                try {
                    FileOutputStream fos=new FileOutputStream("/mnt/sdcard/dcim/相机/G150831_"+System.currentTimeMillis()+".png");
                    bitmap.compress(Bitmap.CompressFormat.PNG,85,fos);

                    //重新继续拍照
                    camera.stopPreview();
                    camera.startPreview();

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

            }
        });
    }

你可能感兴趣的:(安卓,android,服务)