Android——拍照保存在图库
这个功能的实现使用的技术比较落后,但是对于我这样的初学者来说还是足够的
直接上代码
整个代码都在MainAvtivity中,测试使用的activity十分简单,最重要的是有两个id即可。
package com.example.takephotos; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.PixelFormat; import android.hardware.Camera; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.Toast; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class MainActivity extends AppCompatActivity { private Camera camera;//摄像头对象 private boolean isPreview=false;//是否为预览状态 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏显示 if(!android.os.Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED )){ Toast.makeText(this,"请安装sd卡",Toast.LENGTH_SHORT).show(); } //打开摄像头并预览 SurfaceView surfaceView=findViewById(R.id.surfaceView);//用于显示摄像头预览 SurfaceHolder surfaceHolder = surfaceView.getHolder(); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); Button preview=findViewById(R.id.preview); preview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(!isPreview){ camera=Camera.open();//打开摄像头 isPreview=true; } try { camera.setPreviewDisplay(surfaceHolder); Camera.Parameters parameters=camera.getParameters(); parameters.setPictureFormat(PixelFormat.JPEG); parameters.set("jpeg-quality",80); camera.setParameters(parameters); camera.startPreview(); camera.autoFocus(null); } catch (IOException e) { e.printStackTrace(); } } }); //实现拍照功能 Button takephoto=findViewById(R.id.takephoto); takephoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (camera!=null){ camera.takePicture(null,null,jpeg); } } }); } Camera.PictureCallback jpeg = new Camera.PictureCallback() { @Override public void onPictureTaken(byte[] bytes, Camera camera) { Bitmap bitmap= BitmapFactory.decodeByteArray(bytes,0,bytes.length); camera.stopPreview();//停止预览 isPreview=false; File appDir=new File(Environment.getExternalStorageDirectory(),"/DCIM/Camera"); String fileName=System.currentTimeMillis()+".jpg"; File file=new File(appDir,fileName); try { FileOutputStream fos=new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //将相片插入系统图库 try { MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), file.getAbsolutePath(),fileName,null); } catch (FileNotFoundException e) { e.printStackTrace(); } //图库更新 MainActivity.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+""))); Toast.makeText(MainActivity.this,"保存至"+file,Toast.LENGTH_LONG).show(); resetCamera();//重新预览 } }; private void resetCamera() { if(!isPreview){ camera.startPreview();//预览 isPreview=true; } } @Override protected void onPause() { super.onPause(); if(camera!=null){ camera.stopPreview(); camera.release();//释放资源 } } }
注:这里面使用到了sd卡,并不一定使用真机,我使用的是逍遥模拟器(这个无论打游戏还是学习都挺好用的)
实现效果:电脑摄像头是前置的