camera - Android: mPrevCallback to JPG, results in black pictures -
i trying capture preview of camera on surfaceview ,to save jpeg in internal memory. found code here on site, want saves image sd card. changed that, , came following code.
camera.previewcallback mprevcallback = new camera.previewcallback() { @override public void onpreviewframe( byte[] data, camera cam ) { //log.d(tag, "frame"); camera.parameters parameters = cam.getparameters(); int format = parameters.getpreviewformat(); //log.d(tag, "format:" + format); //yuv formats require more conversion if (format == imageformat.nv21 || format == imageformat.yuy2 || format == imageformat.nv16) { int w = parameters.getpreviewsize().width; int h = parameters.getpreviewsize().height; // yuv image yuvimage yuv_image = new yuvimage(data, format, w, h, null); // convert yuv jpeg rect rect = new rect(0, 0, w, h); bytearrayoutputstream output_stream = new bytearrayoutputstream(); yuv_image.compresstojpeg(rect, 100, output_stream); byte[] byt = output_stream.tobytearray(); fileoutputstream outstream = null; try { outstream = new fileoutputstream("/data/data/com.example.max.camtest/files/test"+system.currenttimemillis()+".jpg"); outstream.write(byt); outstream.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } { } } } };
the preview shown on surfaceview , mprevcallback triggered.it saves pictures have diffrent sizes (250~500kb) black. when try capture picture camera.takepicture function black.
what doing wrong? how can debug this? thanks!
use intent take picture
intent intent = new intent(mediastore.action_image_capture); file f = new file(android.os.environment.getexternalstoragedirectory(), appinfo.getinstance().getcurrentloginuserinfo().getid()+".jpg"); intent.putextra(mediastore.extra_output, uri.fromfile(f)); intent.putextra("return-data", true); startactivityforresult(intent, 1);
and on activity result.... note bitmap bitmap = getscaledbitmap(uri.getpath(), 200, true);
200 max image size.
if(requestcode == 1) { string base = environment.getexternalstoragedirectory().getabsolutepath().tostring(); final string imgpath = base + "/" +appinfo.getinstance().getcurrentloginuserinfo().getid()+".jpg"; file file = new file(imgpath); if (file.exists()) { uri uri = uri.fromfile(file); log.d(tag, "image uri path: " + uri.getpath()); bitmap bitmap = getscaledbitmap(uri.getpath(), 200, true); }}
this method ll return image bitmap after resizing it-
private bitmap getscaledbitmap(string imagepath, float maximagesize, boolean filter) { fileinputstream in; bufferedinputstream buf; try { in = new fileinputstream(imagepath); buf = new bufferedinputstream(in); bitmap realimage = bitmapfactory.decodestream(buf); float ratio = math.min( (float) maximagesize / realimage.getwidth(), (float) maximagesize / realimage.getheight()); int width = math.round((float) ratio * realimage.getwidth()); int height = math.round((float) ratio * realimage.getheight()); bitmap newbitmap = bitmap.createscaledbitmap(realimage, width, height, filter); return newbitmap; } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } return null; }
now have scaled bitmap image.
hope ll you.
Comments
Post a Comment