我正在使用GPUImage安卓库在相机预览上应用滤镜,并在拍照后使用滤镜保存图像。问题是,当我拍摄图片时,我无法使用滤镜获取图像。 我使用以下代码:
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
mGPUImage.setImage(bitmap);
bitmap = mGPUImage.getBitmapWithFilterApplied();
saveImage(bitmap);
}
GPUImage的库页面(https://github.com/CyberAgent/android-gpuimage/#sample-code)中的示例代码说:
预览:
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
Uri imageUri = ...;
mGPUImage = new GPUImage(this);
mGPUImage.setGLSurfaceView((GLSurfaceView) findViewById(R.id.surfaceView));
mGPUImage.setImage(imageUri); // this loads image on the current thread, should be run in a thread ?? (can't understand this line)
mGPUImage.setFilter(new GPUImageSepiaFilter());
// Later when image should be saved saved:
mGPUImage.saveToPictures("GPUImage", "ImageWithFilter.jpg", null);
}
即使在他们的样本中,我也无法使用过滤器保存图像。 请有人向我解释一下吗?
答案 0 :(得分:2)
使用此代码
///Call this method when you are ready to save image///
//I am calling on click of save button//
private void takePhoto() {
releaseCamera();
new AsyncTask<Void, Void, Void>() {
Bitmap bitmap;
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
bitmap = gpuImageView.capture();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
protected Void doInBackground(Void... params) {
File dir = Util.getCameraDirectory(); // directory where you want to save image
if (!dir.exists()) {
dir.mkdirs();
}
String filename = getString(R.string.app_name) + System.currentTimeMillis() + ".jpg";
File file = new File(dir, filename);
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
Intent intent =
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
} catch (Exception exception) {
exception.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
prepareCamera();
Toast.makeText(MyActivity.this, R.string.msg_after_save, Toast.LENGTH_SHORT).show();
}
}.execute();
}
private void prepareCamera() {
camera = Camera.open(cameraId);
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = getOptimalPreviewSize(camera.getParameters().getSupportedPreviewSizes(), getWindowManager().getDefaultDisplay().getWidth(), getWindowManager().getDefaultDisplay().getHeight());
parameters.setPreviewSize(size.width, size.height);
if (parameters.getSupportedFocusModes().contains(
Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {
parameters
.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
}
parameters.setPreviewFormat(ImageFormat.NV21);
camera.setParameters(parameters);
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
int orientation = getCameraDisplayOrientation(info);
boolean flipHorizontal = info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT;
gpuImageView.getGPUImage().setUpCamera(camera, orientation,
flipHorizontal, false);
}
private void releaseCamera() {
if (camera != null) {
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release();
camera = null;
}
}
如果您有任何进一步的问题,请告诉我。
此外,再次检查清单中的写入权限