问题
I am using GPUImage android library to apply filters on camera preview and save the image with filters applied after take a picture. The problem is that when I took the pictute, I can't get the image with the filters. I am using the following code:
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
mGPUImage.setImage(bitmap);
bitmap = mGPUImage.getBitmapWithFilterApplied();
saveImage(bitmap);
}
The sample code in GPUImage's library page (https://github.com/CyberAgent/android-gpuimage/#sample-code) says:
With preview:
@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);
}
Even in their sample I can't save the image with filter. Please somebody could explain it to me?
回答1:
Use this code
///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;
}
}
Let me know if you face any further issue.
Also, check again for write permission in manifest
来源:https://stackoverflow.com/questions/39154572/save-applied-filter-in-camera-preview-with-gpuimage