费尽周折终于把这块代码凑齐了,以后可以直接用了,需要的小盆友可以参考一下。
private void takePic() {
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bmptemp=bmptemp= BitmapFactory.decodeByteArray(data,0,data.length);
//对图片进行旋转处理
Matrix matrix = new Matrix();
matrix.reset();
matrix.postRotate(90);
Bitmap bMapRotate = Bitmap.createBitmap(bmptemp, 0, 0,bmptemp.getWidth(), bmptemp.getHeight(), matrix, true);
File filePath = new File(Environment.getExternalStorageDirectory(), "myCamera");
if(!filePath.exists()) {
filePath.mkdirs();
}
File fileName = new File(filePath, System.currentTimeMillis() + ".jpg");
try {
fileName.createNewFile();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
bMapRotate.compress(Bitmap.CompressFormat.JPEG, 100, bos);//将图片压缩到流中
bos.flush();//输出
bos.close();//关闭
Intent intent=new Intent();
intent.putExtra("filepath",fileName.toString());
setResult(0,intent);
finish();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
本文介绍了一段用于Android应用中的代码,该代码实现了通过调用摄像头拍照,并将拍摄的照片保存到本地的功能。具体包括设置拍照回调、图片旋转处理、文件路径设定及文件创建等关键步骤。
1349

被折叠的 条评论
为什么被折叠?



