Unity3d upload and crop images locally-----Android platform

Source: Internet
Author: User

Unity3d upload and crop images locally-----Android platform

Note: Engine version unity4.x

Recent project needs, need to do the user picture, requirements:

1. can upload from local

2. Local cropping

3. Compression control size

4. Implementation of Pc/android/ios on three platforms

I've been doing it for days.

uploading from the local will use the Android System feature, open the album and take pictures with the camera, so unity and Android interaction is necessary.

unity-android can refer to Xuanhusung's blog http://www.xuanyusong.com/archives/676package com.cheerflame.sdrn;



I took a look at some of the posts and then sorted out a picture processing class, directly importing this class to invoke its method.

Usage (3 steps):
1.//creating a picture processing object in Mainactivity

Imgmanage = new Headimgmanage (this);

2.//calls this interface in unity, invokes the Takephoto method of the picture processing class in the interface, and when choose = 0, turn on the camera and open the photo when choose=1.

public void Takephoto (int choose) {
Imgmanage.takephoto (choose);
}

3.//receiving an event request callback in Mainactivity

protected void Onactivityresult (int requestcode, int resultcode, Intent data) {log.i ("TEST", "Requestcode =" + reques        Tcode);         if (ResultCode = = none) {log.i ("TEST", "ResultCode = = none");        Return            } if (Requestcode = = photohraph) {log.i ("TEST", "Take photo complete");            File picture = new file (environment.getexternalstoragedirectory () + "/temp.jpg");        Imgmanage.startphotozoom (Uri.fromfile (picture));        } if (data = = null) {LOG.I ("TEST", "data = = null");        Return        }//Read album Zoom picture if (Requestcode = = photozoom) {log.i ("TEST", "read album complete");        Imgmanage.startphotozoom (Data.getdata ());            }//Processing result if (Requestcode = = photoresoult) {Bundle extras = Data.getextras ();            if (extras! = null) {Bitmap photo = extras.getparcelable ("Data");   try {imgmanage.savebitmap (photo);                } catch (IOException e) {//TODO auto-generated Cat                       CH Block e.printstacktrace ();    }}} super.onactivityresult (Requestcode, ResultCode, data);   }

Processing class:

Package Com.cheerflame.sdrn;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Com.unity3d.player.unityplayer;import Com.unity3d.player.unityplayeractivity;import Android.content.intent;import Android.graphics.bitmap;import Android.net.uri;import Android.os.environment;import Android.provider.mediastore;import Android.util.Log;public    Class headimgmanage{unityplayeractivity unityactivity;public static final int NONE = 0; public static final int photohraph = 1;//camera public static final int photozoom = 2;      Scale public static final int photoresoult = 3;//result public static final String image_unspecified = "image/*";    Public final static String file_name = "Image.jpg";        Public final static String Data_url = "/data/data/";        Public Headimgmanage (unityplayeractivity activity) {unityactivity = activity;} public void Takephoto (int choose) {if (choose = = 0) {Intent intent = new Intent (mediastore.action_image_capture); Intent.putextra (Mediastore.extra_output, Uri.fromfile (New File (Environment.getexternalstoragedirectory (), "            Temp.jpg ")));        Unityactivity.startactivityforresult (Intent, photohraph);            } else {Intent Intent = new Intent (Intent.action_pick, NULL);            Intent.setdataandtype (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image_unspecified);        Unityactivity.startactivityforresult (Intent, photozoom);            }} public void Startphotozoom (Uri uri) {log.i ("TEST", "start cropping" + uri);        Intent Intent = new Intent ("Com.android.camera.action.CROP");        Intent.setdataandtype (URI, image_unspecified);        Intent.putextra ("Crop", "true");        Aspectx Aspecty is a proportional Intent.putextra ("Aspectx", 1) of the wide height;        Intent.putextra ("Aspecty", 1);        Outputx Outputy is a cropped picture with a wide height of Intent.putextra ("Outputx", 300);        Intent.putextra ("Outputy", 300); InteNt.putextra ("Return-data", true);    Unityactivity.startactivityforresult (Intent, Photoresoult);                } public void Savebitmap (Bitmap Bitmap) throws IOException {log.i ("TEST", "Save File");                FileOutputStream fOut = null;                Note String Path = "/mnt/sdcard/android/data/com.cheerflame.sdrn/files";                          try {//See if this path exists,//If it does not have this path,//create this path                          File DestDir = new file (path);                          if (!destdir.exists ()) {destdir.mkdirs ();                        } fOut = new FileOutputStream (path + "/" + file_name);                        LOG.I ("TEST", "Save path:" + path + "/" + file_name);                        Unityplayer.unitysendmessage ("Camera", "Headimage", "success!");                LOG.I ("TEST", "success"); } catch (FilenoTfoundexception e) {e.printstacktrace (); }//Writes the bitmap object to the local path, unity is going to the same path to read the file bitmap.compress (Bitmap.CompressFormat.JPEG, FOu                T);                try {fout.flush ();                } catch (IOException e) {e.printstacktrace ();                } try {fout.close ();                } catch (IOException e) {e.printstacktrace (); }        }}

The "/mnt/sdcard/android/data/com.xxxx.xxx/files" directory is the same as the UnityEngine.Application.persistentDataPath get path in unity, That is, sandbox files, the operation of the sandbox file online very much, I do not say much here

Bitmap.compress (Bitmap.CompressFormat.JPEG, fOut); This parameter 10, is 0-100 to choose the image quality function, generally from the album after the picture, the picture size is about 30-40k, obviously too big, 10 words are compressed to the original 1/10, that is, 3-4k is very small


iOS Blog after the add

Unity3d upload and crop images locally-----Android platform

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.