============ Problem Description ============
public class Photographactivity extends baseactivity implements
Seekbar.onseekbarchangelistener, Onclicklistener, Runnable {
Private SeekBar zoomset;//Adjust focal length
Private ImageView Takepic, back, flash;//button
Private final int flash_auto = 0;//Auto Flash
Private final int flash_off = 1;//Flash off
Private final int flash_on = 2; Flash Open
private int flashimg[] = {r.drawable.light_auto_confirm,
R.drawable.light_off_confirm, r.drawable.light_on_confirm};//flash icon
private int zoom, maxzoom;//zoom now focal length, Maxzoom max focal length
Private Boolean previews = False, Flashopen = false;//preview Flashopen Flash
Private camera camera;//Hardware cameras
Private Surfaceview camerabg;//Camera preview
@SuppressWarnings ("unused")
Private Uri Videofilepath;
Popupwindow and Alertdialog are both the contents of the android dialog, unlike Popupwindow, which is a blocking dialog box
Popupwindow Popupwindow;
Camera.parameters parameters;//Camera Parameter set
@Override
public void OnCreate (Bundle savedinstancestate) {
if (! Environment.getexternalstoragestate (). Equals (
environment.media_mounted)) {//SD card allows read and write
Toast.maketext (Photographactivity.this, "SD card is not plugged in or not read and write",
Toast.length_long). Show ();
}
window window = GetWindow ();
As long as this window is visible to the user, keep the device's screen open and bright.
Window.addflags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Choose to support semitransparent mode, which is used in surfaceview activity.
Window.setformat (pixelformat.translucent);
Baseactivity.isfullsreen = true;
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_photograph);
Init ();
}
@SuppressLint ("Newapi")
private void init () {
Takepic = (ImageView) Findviewbyid (r.id.btn_picture);//Photo
back = (ImageView) Findviewbyid (r.id.btn_backcapter);//Return
Flash = (ImageView) Findviewbyid (R.id.btn_flash);//adjust the flash effect
Zoomset = (SeekBar) Findviewbyid (r.id.seekbar_focal);//Adjust focal length
CAMERABG = (Surfaceview) Findviewbyid (R.id.camrea);//Camera Preview
High width of fixed surfaceview
Camerabg.getholder (). Setfixedsize ((int) Baseactivity.screenw,
(int) BASEACTIVITY.SCREENH);
Set the Surfaceview without maintaining the buffer itself
Camerabg.getholder (). SetType (Surfaceholder.surface_type_push_buffers);
Set the callback function for Surfaceview
Camerabg.getholder (). Addcallback (New Surfacecallback ());
Zoomset.setonseekbarchangelistener (this);
Takepic.setonclicklistener (this);
Back.setonclicklistener (this);
Flash.setonclicklistener (this);
Camerabg.setonclicklistener (this);
}
Private Final class Surfacecallback implements Surfaceholder.callback {
@Override
public void surfacecreated (Surfaceholder holder) {
if (camera! = null) {
Camera.release ();//Camera release
}
Camera = Camera.open ();//Turn on cameras
Camera.setdisplayorientation (90);//Rotating Lens
Parameters = Camera.getparameters ();//Get camera parameter set
if (parameters.getflashmode () = = null) {
Flash.setimagebitmap (Bitmapfactory.decoderesource (
Getresources (), Flashimg[flash_off]);
Toast.maketext (Photographactivity.this, "No Flash",
Toast.length_short). Show ();
} else {
Parameters.setflashmode (Camera.Parameters.FLASH_MODE_AUTO);
parameters.setpreviewsize (100, 120);//Set the size of the preview photo
Parameters.setpictureformat (imageformat.jpeg);//Set Picture format
Parameters.setpicturesize (100, 120);//Set the size of the photo
Camera.setparameters (parameters);
}
Maxzoom = Parameters.getmaxzoom ();//Get maximum pixels
Zoomset.setmax (maxzoom);//Set maximum pixels
try {
Camera.setpreviewdisplay (Camerabg.getholder ());//Set Camera preview
} catch (IOException e) {
E.printstacktrace ();
}
Camera.startpreview ();//Start preview
Preview = true;
}
@Override
public void surfacechanged (surfaceholder holder, int format, int width,
int height) {
}
@Override
public void surfacedestroyed (Surfaceholder holder) {
if (camera! = null) {
if (preview) {
Camera.stoppreview ();
}
Camera.release ();
}
}
}
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (camera! = null && event.getrepeatcount () = = 0) {
Switch (keycode) {
Case Keyevent.keycode_camera:
Case Keyevent.keycode_dpad_center:
Takpicture ();
Break
Case KEYEVENT.KEYCODE_VOLUME_UP:
Zoom = (zoom + 5) < Maxzoom? (Zoom + 5): Maxzoom;
zoomset.setprogress (zoom);
Break
Case Keyevent.keycode_volume_down:
Zoom = (zoom-5) > 0? (zoom-5): 0;
zoomset.setprogress (zoom);
Break
Case Keyevent.keycode_back:
if (Flashopen) {
Popupwindow.dismiss ();
} else {
Finish ();
}
}
}
return true;
}
Private Final class Takepicturecallback implements Picturecallback {
@Override
public void Onpicturetaken (byte[] data, Camera camera) {}
}
public void Takpicture () {
Camera.autofocus (null);//Autofocus
Camera.takepicture (NULL, NULL, NULL, New Takepicturecallback ());
}
@Override
public void onprogresschanged (SeekBar SeekBar, int progress,
Boolean Fromuser) {
Zoom = Zoomset.getprogress ();
Parameters.setzoom (zoom);
Camera.setparameters (parameters);
}
@Override
public void Onstarttrackingtouch (SeekBar SeekBar) {
}
@Override
public void Onstoptrackingtouch (SeekBar SeekBar) {
}
@Override
public void OnClick (View v) {
Switch (V.getid ()) {
Case R.id.btn_picture:
Takpicture ();
Break
Case R.id.btn_backcapter:
if (Flashopen) {
Popupwindow.dismiss ();
} else {
Finish ();
}
Break
Case R.id.btn_flash:
if (parameters.getflashmode () = = null) {
Toast.maketext (Photographactivity.this, "No Flash",
Toast.length_short). Show ();
} else if (parameters.getflashmode () = = Camera.Parameters.FLASH_MODE_AUTO) {
Flash.setimagebitmap (Bitmapfactory.decoderesource (
Getresources (), Flashimg[flash_off]);
Parameters.setflashmode (Camera.Parameters.FLASH_MODE_OFF);
Camera.setparameters (parameters);
} else if (parameters.getflashmode () = = Camera.Parameters.FLASH_MODE_OFF) {
Flash.setimagebitmap (Bitmapfactory.decoderesource (
Getresources (), flashimg[flash_on]);
Parameters.setflashmode (Camera.Parameters.FLASH_MODE_ON);
Camera.setparameters (parameters);
} else if (parameters.getflashmode () = = Camera.Parameters.FLASH_MODE_ON) {
Flash.setimagebitmap (Bitmapfactory.decoderesource (
Getresources (), Flashimg[flash_auto]);
Parameters.setflashmode (Camera.Parameters.FLASH_MODE_AUTO);
Camera.setparameters (parameters);
}
Break
Case R.id.camrea:
Camera.autofocus (NULL);
Break
}
}
@Override
protected void OnPause () {
Super.onpause ();
Zoomset.setprogress (0);//Let Seekbar bar Initialize
}
public void Run () {
while (true) {
if (camera! = null) {
Camera.autofocus (NULL);
}
try {
Thread.Sleep (400);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}
Code that blocks the Scarlet letter part of the program onceparameters.setpreviewsize (100, 120);//Set the size of the preview photo
Parameters.setpictureformat (imageformat.jpeg);//Set Picture format
Parameters.setpicturesize (100, 120);//Set the size of the photo
will be able to achieve the normal mobile phone camera function
If you do not block [color= #FF0000] parameters.setpreviewsize (100, 120);//Set the size of the preview photo
Parameters.setpictureformat (imageformat.jpeg);//Set Picture format
Parameters.setpicturesize (100, 120);//Set the size of the photo
[/color
Log logs will report the following error:
09-24 16:58:52.385:e/androidruntime (25089):FATAL Exception:main
09-24 16:58:52.385:e/androidruntime (25089):Java.lang.RuntimeException:setParameters failed
09-24 16:58:52.385:e/androidruntime (25089):At android.hardware.Camera.native_setParameters (native Method)
09-24 16:58:52.385:e/androidruntime (25089):At android.hardware.Camera.setParameters (camera.java:1449)
09-24 16:58:52.385:e/androidruntime (25089):At app.example.takephoto.photographactivity$surfacecallback.surfacecreated (photographactivity.java:117)
09-24 16:58:52.385:e/androidruntime (25089):At Android.view.SurfaceView.updateWindow (surfaceview.java:543)
09-24 16:58:52.385:e/androidruntime (25089): at android.view.surfaceview.access$000 (surfaceview.java:81)
09-24 16:58:52.385:e/androidruntime (25089):At Android.view.surfaceview$3.onpredraw (surfaceview.java:169)
09-24 16:58:52.385:e/androidruntime (25089):At Android.view.ViewTreeObserver.dispatchOnPreDraw (viewtreeobserver.java:671)
09-24 16:58:52.385:e/androidruntime (25089): at Android.view.ViewRootImpl.performTraversals (viewrootimpl.java:1892 )
09-24 16:58:52.385:e/androidruntime (25089):At android.view.ViewRootImpl.doTraversal (viewrootimpl.java:1070)
09-24 16:58:52.385:e/androidruntime (25089):At Android.view.viewrootimpl$traversalrunnable.run (viewrootimpl.java:4296)
09-24 16:58:52.385:e/androidruntime (25089): at Android.view.choreographer$callbackrecord.run (Choreographer.java : 725)
That is to say the above parameters call three methods of any one of the mask will be error, the younger brother can not find a solution. To kneel and beg the great God for guidance
============ Solution 1============
I have encountered this problem before, I go back to check the code, hehe ~
============ Solution 2============
Parameter settings failed, your parameters are not right, please take a closer look at the use of parameters.setpreviewsize.
Android Camera Parameters method Error, ask for advice