Hi,
I have an Dialog with a custom view. This view has a SurfaceView which
shows a camera preview. However, the preview is very flickering.
The taken photo does not contain flicker. It looks as it should.
Why does the preview flicker? (The camera does work :-) )
This is my custom view:
---------
public class CameraView extends LinearLayout implements
SurfaceHolder.Callback, OnClickListener, PictureCallback {
private SurfaceView surfView;
private SurfaceHolder previewHolder;
private Camera camera;
OnProfilePictureTakenListener onProfilePictureTakenListener = null;
public CameraView(Context context) {
super(context);
setOnClickListener(this);
surfView = new SurfaceView(getContext());
addView(surfView);
previewHolder = surfView.getHolder();
previewHolder.addCallback(this);
previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int
width,
int height) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(previewHolder);
} catch(Throwable t) {
Log.e("surfaceCallback", "Exception", t);
Toast.makeText(getContext(), t.getMessage(),
Toast.LENGTH_LONG).show
();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
@Override
public void onClick(View v) {
takePicture();
}
private void takePicture() {
camera.stopPreview();
camera.takePicture(null, null, this);
}
@Override
public void onPictureTaken(byte[] data, Camera camera) {
new SavePhotoTask().execute(data);
camera.startPreview();
}
public void setOnPictureTakenListener(OnProfilePictureTakenListener
lis) {
onProfilePictureTakenListener = lis;
}
class SavePhotoTask extends AsyncTask<byte[], String, String> {
@Override
protected String doInBackground(byte[]... jpeg) {
File photo = new
File(Environment.getExternalStorageDirectory(),
"/.addMePictures/" + System.currentTimeMillis());
if(photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos = new
FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
} catch(IOException e) {
e.printStackTrace();
}
if(onProfilePictureTakenListener != null) {
onProfilePictureTakenListener.onProfilePictureTaken(photo);
}
return null;
}
}
public interface OnProfilePictureTakenListener {
public abstract void onProfilePictureTaken(File imgFile);
}
}
--------
Thank you very much!
//Kaloer
--
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en