php - Can anyone help me capture an image, display it in imageView, store in gallery, upload that image to server? -
i want camera capture, image store in gallery, display in image view on screen , upload image server using php,json. have json parser code works. image gets saved as"temp" , gets overwritten(which don't want because want images in gallery) posting code below, please guide me.
public class camactivity extends activity { private imageview img; int boundboxindp; private static final int camera_request = 1888; button bt1,bt2,btn; contentvalues values; int serverresponsecode = 0; progressdialog dialog = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_cam); string path = android.os.environment.getexternalstoragedirectory()+ file.separator + "sdcard" + file.separator + "sdcard"; img = (imageview)findviewbyid(r.id.imageview1);// assign imageview bitmapdrawable drawable = (bitmapdrawable) img.getdrawable();//get image drawable , assign bitmap drawable final bitmap bmap = drawable.getbitmap();//assign bitmap drawable bmap bitmap image variable bytearrayoutputstream outstream = new bytearrayoutputstream(); bmap.compress(bitmap.compressformat.png, 100, outstream);// , line above can ignored //start camera on launch of page intent camera_intent = new intent(android.provider.mediastore.action_image_capture); // save code here file f = new file(android.os.environment.getexternalstoragedirectory(), "temp.png"); camera_intent.putextra(mediastore.extra_output, uri.fromfile(f)); //save code ends startactivityforresult(camera_intent, camera_request); //start camera on launch of page ends bt1=(button)findviewbyid(r.id.button1);//button go next page bt1.setonclicklistener (new onclicklistener()//click button travel next page { public void onclick(view v) { img = (imageview)findviewbyid(r.id.imageview1); img.setscaletype(scaletype.matrix); bitmapdrawable drawable = (bitmapdrawable) img.getdrawable(); drawable.setfilterbitmap(true); final bitmap bmap = drawable.getbitmap(); //convert starts works //convert bitmap grayscale bitmap imgtogreyscale; imgtogreyscale = tograyscale(bmap); //convert threshold bitmap imgtoblackwhite; imgtoblackwhite = converttothreshold(imgtogreyscale); //set imageview imageview imgbit; imgbit = (imageview) findviewbyid(r.id.imageview1); imgbit.getimagematrix(); imgbit.setimagebitmap(imgtoblackwhite); //conversion ends works intent intent2 = new intent(camactivity.this,qrcodeactivity.class); startactivity(intent2); } }); } // function starts-------------------------------------------------------------------------------------------- //threshold function -works- public bitmap converttothreshold(bitmap anythingbmap) { int width = anythingbmap.getwidth(); int height = anythingbmap.getheight(); int threshold = 120; for(int x=0;x<width;x++){ for(int y=0;y<height;y++){ int pixel = anythingbmap.getpixel(x, y); int gray = color.red(pixel); if(gray < threshold){ anythingbmap.setpixel(x, y, 0xff000000); } else{ anythingbmap.setpixel(x, y, 0xffffffff); } } } return anythingbmap; } //threshold function ends // grayscale function -works- public bitmap tograyscale(bitmap bmporiginal){ int width, height; height = bmporiginal.getheight(); width = bmporiginal.getwidth(); bitmap bmpgrayscale = bitmap.createbitmap(width, height, bitmap.config.rgb_565); canvas c = new canvas(bmpgrayscale); paint paint = new paint(); colormatrix cm = new colormatrix(); cm.setsaturation(0); colormatrixcolorfilter f = new colormatrixcolorfilter(cm); paint.setcolorfilter(f); c.drawbitmap(bmporiginal, 0, 0, paint); return bmpgrayscale; } // grayscale function ends //functions ends---------------------------------------------------------------------------------------- //onresult activity image protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == camera_request && resultcode == result_ok) { { file f = new file(environment.getexternalstoragedirectory().tostring()); (file temp : f.listfiles()) //click , store { if (temp.getname().equals("temp.png")) { f = temp; break; } } try { bitmap bitmap; bitmapfactory.options bitmapoptions = new bitmapfactory.options(); bitmap = bitmapfactory.decodefile(f.getabsolutepath(),bitmapoptions); bytearrayoutputstream outstream = new bytearrayoutputstream();// bitmap.compress(bitmap.compressformat.png, 100, outstream); img.setimagebitmap(bitmap); bitmap greyscalebitmap =tograyscale(bitmap);//upload upload(greyscalebitmap); bitmap thresholdbitmap= converttothreshold(bitmap);//upload string path = android.os.environment.getexternalstoragedirectory()+ file.separator + "sdcard" + file.separator + "sdcard"; //f.delete(); outputstream outfile = null; file file = new file(path, string.valueof(system.currenttimemillis()) + ".png" +""); try { outfile = new fileoutputstream(file); outfile.flush(); outfile.close(); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } } catch (exception e) { e.printstacktrace(); } } } } private void upload(bitmap greyscalebitmap) { bytearrayoutputstream bao = new bytearrayoutputstream(); greyscalebitmap.compress(bitmap.compressformat.jpeg, 90, bao); httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost( "http://example.com/imagestore/post"); multipartentity entity = new multipartentity( httpmultipartmode.browser_compatible ); byte [] ba = bao.tobytearray(); try { entity.addpart("img", new stringbody(new string(bao.tobytearray()))); httppost.setentity(entity); } catch (unsupportedencodingexception e1) { // todo auto-generated catch block e1.printstacktrace(); } // execute http post request httpresponse response = null; try { response = httpclient.execute(httppost); } catch (clientprotocolexception e) { } } }
the php code below
<?php require("config.inc.php"); $target_path = "/uploads/"; $base=$_request['image']; $name=$_request['cmd']; echo $base; echo $name; // base64 encoded utf-8 string $binary=base64_decode($base); // binary, utf-8 bytes header('content-type: bitmap; charset=utf-8'); // print($binary); //$thefile = base64_decode($image_data); $file = fopen($target_path, 'wb'); $file = fopen($name, 'wb'); fwrite($file, $binary); fclose($file); //move_uploaded_file($file,$target_path.$name); copy($name,$target_path.$name); ?>
the problem in php code mistakenly override $file
object
$file = fopen($target_path, 'wb'); $file = fopen($name, 'wb');
should replaced following:
$file = fopen($target_path.$name, 'wb');
you can append random number i.e using rand()
or timestamp i.e. time()
filename
Comments
Post a Comment