wxr
2020-06-15 b8e94316e41eba72d927d5ca7d931b26139ee8ff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
using System;
using Android.Content;
using Android.App;
using Android.OS;
using Android.Graphics;
using Android.Widget;
using Android.Provider;
using Java.IO;
using Android.Graphics.Drawables;
 
namespace Shared
{
    [Activity(MainLauncher = false, Theme = "@android:style/Theme.NoTitleBar.Fullscreen")]
    internal class CameraActivity : Android.App.Activity
    {
        bool isZoom;
        protected override void OnCreate(Android.OS.Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            isZoom = Intent.Extras.GetBoolean("IsZoom", true);
            if (Intent.Extras.GetInt("Type") == Camera.TypePicture)
            {
                var intent = new Intent(Intent.ActionPick);
                intent.SetType("image/*");//相片类型  
                StartActivityForResult(intent, Camera.TypePicture);
            }
            else
            {
                if (Android.OS.Environment.ExternalStorageState == Android.OS.Environment.MediaMounted)
                {
                    var getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
                    StartActivityForResult(getImageByCamera, Camera.TypeCamera);
                }
                else
                {
                    Toast.MakeText(Shared.Application.Activity, "Makue Sure SD Card", ToastLength.Long).Show();
                }
            }
        }
 
 
 
 
        /// <summary>
        /// 处理选择或者拍照的图片
        /// </summary>
        /// <param name="requestCode">Request code.</param>
        /// <param name="resultCode">Result code.</param>
        /// <param name="data">Data.</param>
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            try
            {
                base.OnActivityResult(requestCode, resultCode, data);
                if (Camera._callback == null)
                {
                    return;
                }
                if (resultCode != Result.Ok)
                {
                    Camera._callback(null);
                    return;
                }
 
                var path = System.IO.Path.Combine(Shared.Application.RootPath, Camera._fileName);
                var uri = data.Data;
                if (uri == null)
                {
                    var bundle = data.Extras;
                    if (bundle == null)
                    {
                        Toast.MakeText(Shared.Application.Activity, "Error", ToastLength.Long).Show();
                        path = null;
                    }
                    else
                    {
                        compressAndGenImage((Bitmap)bundle.Get("data"), path, 200);
                    }
                }
                else
                {
                    var photoPath = uri.Path;
                    if (!System.IO.File.Exists(photoPath))
                    {
                        //获取照片路径
                        string[] filePathColumn = { MediaStore.Audio.AudioColumns.Data };
                        var cursor = Shared.Application.Activity.ContentResolver.Query(uri, filePathColumn, null, null, null);
                        cursor.MoveToFirst();
                        photoPath = cursor.GetString(cursor.GetColumnIndex(filePathColumn[0]));
                        cursor.Close();
                    }
 
                    compressAndGenImage(BitmapFactory.DecodeFile(photoPath), path, 200);
                }
                Camera._callback(path);
            }
            finally
            {
                Finish();
            }
        }
        void compressAndGenImage(Bitmap bitmap, string outPath, int maxSize)
        {
            if (isZoom)
            {
                var width = bitmap.Width;
                var height = bitmap.Height;
                //Shared.HDLUtils.WriteLine("原宽度=" + bitmap.Width + "高度=" + bitmap.Height);
                var noScale = width * 1.0f / height;
                var scale = 16.0f / 9.0f;
                float x = 0, y = 0, w = width, h = height;
                if (scale < noScale)
                {
                    w = height * scale;
                    x = (width - w) / 2;
                }
                else
                {
                    h = width / scale;
                    y = (height - h) / 2;
                }
                var matrix = new Matrix();
                matrix.PostScale(Shared.Application.GetRealWidth(640) * 1.0f / w, Shared.Application.GetRealHeight(360) * 1.0f / h);
                bitmap = Bitmap.CreateBitmap(bitmap, (int)x, (int)y, (int)w, (int)h, matrix, true);
 
                //Shared.HDLUtils.WriteLine("现在宽度=" + bitmap.Width + "高度=" + bitmap.Height);
                var fs = new System.IO.FileStream(outPath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                // scale  
                var options = 100;
                // Store the bitmap into output stream(no compress)  
                bitmap.Compress(Bitmap.CompressFormat.Jpeg, options, fs);
                // Compress by loop  
                while (fs.Length / 1024 > maxSize)
                {
                    // Clean up os  
                    fs.Close();
                    // interval 10  
                    options -= 10;
                    fs = new System.IO.FileStream(outPath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                    bitmap.Compress(Bitmap.CompressFormat.Jpeg, options, fs);
                }
 
                fs.Flush();
                fs.Close();
            }
            else
            {
                var fs = new System.IO.FileStream(outPath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
                // scale  
                var options = 100;
                // Store the bitmap into output stream(no compress)  
                bitmap.Compress(Bitmap.CompressFormat.Jpeg, options, fs);
                fs.Flush();
                fs.Close();
            }
        }
 
    }
    
    /// <summary>
    /// 拍照或者选择图片
    /// </summary>
    public static class Camera
    {
        /// <summary>
        /// 拍照或者读取图片完成时调用的动作,string==null表示用户取消了拍照或者取消选择图片,string!=null,表示图片的路径
        /// </summary>
        internal static Action<string> _callback;
        /// <summary>
        /// 文件名
        /// </summary>
        internal static string _fileName;
        /// <summary>
        /// 选择类型是拍照
        /// </summary>
        internal static readonly int TypeCamera = 0xFF0001;
        /// <summary>
        /// 选择图片
        /// </summary>
        internal static readonly int TypePicture = 0xFF0002;
        /// <summary>
        /// 拍照
        /// </summary>
        /// <param name="callback">回调函数</param>
        /// <param name="fileName">文件名</param>
        public static void TakePicture(Action<string> callback, string fileName,bool isZoom=true)
        {
            (Application.Activity as BaseActivity)?.SetCamera((obj) => { 
                if(obj){
                    _callback = callback;
                    var intent = new Intent(Application.Activity, typeof(CameraActivity));
                    intent.PutExtra("Type", TypeCamera);
                    intent.PutExtra("IsZoom", isZoom);
                    Application.Activity?.StartActivity(intent);
                }
                else{
                    callback?.Invoke(null);
                }
            });
            _fileName = fileName;
            
        }
       
        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="callback">回调函数</param>
        /// <param name="fileName">文件名</param>
        public static void SelectPicture(Action<string> callback, string fileName, bool isZoom = true)
        {
            (Application.Activity as BaseActivity)?.SetImagePermission((obj) =>
            {
                if (obj)
                {
                    _fileName = fileName;
                    _callback = callback;
                    var intent = new Intent(Application.Activity, typeof(CameraActivity));
                    intent.PutExtra("Type", TypePicture);
                    intent.PutExtra("IsZoom", isZoom);
                    Application.Activity?.StartActivity(intent);
                }else{
                    callback?.Invoke(null);
                }
            });
        }
    }
}