Android about picture processing

Source: Internet
Author: User

First, the layout shows the picture
In the layout of the XML layout of the picture when the ImageView, with SRC to specify the location of the picture. As the following code shows, the designation is a picture of the project catalog (/res/drawable) with the Chinese part named Unknown.png.
It is important to note that Android Studio is in the layoutonly PNG format is recognizedImage, even if the JPEG format, only to change the suffix to PNG is not, compile will not pass.

<ImageView                 android:id="@+id/iv_mytest"                android:src="@drawable/unknown" />

However, not equal to the JPEG format of the picture cannot be displayed, we can be processed by the following code to display the interface.

String imgPath = Environment.getExternalStorageDirectory() + "test.jpg";ImageView iv_mytest = (ImageView) findViewById(R.id.iv_mytest);iv_mytest.setVisibility(View.VISIBLE);if(!imgPath.equals("")) {        Bitmap tempBitmap = BitmapFactory.decodeFile(imgPath);        iv_mytest.setImageBitmap(tempBitmap);//显示图片}

Second, the picture after taking pictures
photo process for Get cached picture path, enter the photo screen, photo interface, take photos automatically into the cache picture path, and enter the callback function, the cache image processing (such as rotation scale, etc.) and stored in their own location- > Delete cache path picture. The
specific code looks like this:

Private String Tmpfilename = "";//Call Camera interface private void photograph () {try {//jump to Photo interface S                Tring Sdstatus = Environment.getexternalstoragestate (); if (!sdstatus.equals (environment.media_mounted)) {//detects if SD is available toastutil.showtoastmsgerror (MYTESTAC                        Tivity.this, "SD card isn't avaiable/writeable right now.");                Return                } tmpfilename=gettempfilepath ();                File out = new file (tmpfilename);                Intent intentphote = new Intent (mediastore.action_image_capture);                Intentphote.putextra (mediastore.extra_video_quality, 1);                Uri uri = Uri.fromfile (out);                Gets the original picture that was uncompressed after the photo was taken and saved in the URI path Intentphote.putextra (Mediastore.extra_output, URI);        Startactivityforresult (Intentphote, 1); }catch (Exception e) {}}/** * Gets the original cached picture storage path * @return */private String Gettempfilepath () {//Photo full path       String fileName = "";        Folder path String Pathurl = environment.getexternalstoragedirectory () + "/tmp/";        ImageName = "Mytest.png";        File File = new file (Pathurl);        File.mkdirs ();//Create Folder FileName = Pathurl + imagename; return fileName;} Callback @overridepublic void Onactivityresult (int requestcode, int resultcode, Intent data) after taking a photo {Super.onactivityresu        Lt (Requestcode, ResultCode, data); if (Requestcode = = 1 && resultcode = = ACTIVITY.RESULT_OK) {bitmapfactory.options Options = new Bit                Mapfactory.options (); Options.injustdecodebounds = true;                Set this property be sure to remember to set the value to False Bitmap Bitmap = Bitmapfactory.decodefile (tmpfilename);                Prevent Oom from occurring options.injustdecodebounds = false;                Savemybitmap (IMAGENAME,BITMAP);                File old = new file (environment.getexternalstoragedirectory () + "/tmp/");  Fileutil.deletefile (old);//Delete cached pictures              String resultmsg = "Picture Saved";        Toastutil.showtoastmsgsuccess (this, resultmsg); }}//save image to sd card public void Savemybitmap (string bitname, Bitmap mbitmap) {string thisdate = Formatter_date.format (NE        W Date ()); File f = Fileutil.getfilepath (Environment.getexternalstoragedirectory () + "/rfid/prehairpin/" +thisDate+ "/", BitName)        ;        String Realfilepath = F.getpath ();        FileOutputStream fOut = null;        try {fOut = new FileOutputStream (f);        } catch (Exception e) {e.printstacktrace ();        } Matrix Matrix = new Matrix ();        Scales the image by a fixed size Matrix.postscale (0.3f, 0.3f);        System.out.println (Mbitmap.getwidth () + mbitmap.getheight ());  if (Mbitmap.getheight () < Mbitmap.getwidth ()) {matrix.postrotate (90); Flip 90 degrees} mbitmap = Bitmap.createbitmap (mbitmap, 0, 0, mbitmap.getwidth (), Mbitmap.getheight (), Matrix, True        ); Mbitmap.compress (BiTmap.CompressFormat.PNG, FOut);        try {fout.write ("My Secret Message"). GetBytes ());//I'm here to sneak a few messages to the end of the picture Fout.flush ();        } catch (IOException e) {e.printstacktrace ();        } try {fout.close ();        } catch (IOException e) {e.printstacktrace (); }}

Third, the image processing
Rotation, scaling and other operations we are working through the matrix, and there are many other ways that the matrix can be used for graphics processing, and it is possible to open another story.

Matrix matrix = new Matrix();// 按照固定大小对图片进行缩放matrix.postScale(0.3f, 0.3f);if (mBitmap.getHeight() < mBitmap.getWidth()) {        matrix.postRotate(90);  //翻转90度}mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);

Save the picture, and put the character into the picture (sometimes after taking pictures and want to bind some information with the picture, directly record the file name and worry about being tampered with, think of this way to record some information at the end of the picture file)

String thisDate = formatter_date.format(new Date());File f = FileUtil.getFilePath(Environment.getExternalStorageDirectory() + "/rfid/prehairpin/"+thisDate+"/", bitName);String realfilepath = f.getPath();FileOutputStream fOut = null;try {        fOut = new FileOutputStream(f);} catch (Exception e) {        e.printStackTrace();}//我在这边偷偷给图片结尾藏了一些信息try {        fOut.write(("my secret message").getBytes());        fOut.flush();} catch (IOException e) {        e.printStackTrace();}try {        fOut.close();} catch (IOException e) {        e.printStackTrace();}

Iv. Differences in image formats
PNG, JPEG because of the format of the differences in the internal addition of character information will be different, such as the end of the PNG format is the picture information, so add the words directly at the end to add it, and JPEG no, JPEG at the end of the fixed format information, directly loaded at the end of the image does not affect the display, However, when parsing, the character information parsed by the location offset is not correct.
This piece of content has yet to be studied in depth, and it was only two formats that were tested, and the problem was discovered.

Android about picture processing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.