1
1
package xyz .zpayh .original ;
2
2
3
3
import android .app .Application ;
4
+ import android .content .Context ;
5
+ import android .graphics .BitmapRegionDecoder ;
6
+ import android .net .Uri ;
7
+ import android .support .annotation .NonNull ;
8
+ import android .support .media .ExifInterface ;
9
+ import android .util .Log ;
4
10
11
+ import com .bumptech .glide .Glide ;
12
+ import com .bumptech .glide .RequestManager ;
13
+ import com .bumptech .glide .request .FutureTarget ;
14
+ import com .facebook .binaryresource .BinaryResource ;
15
+ import com .facebook .binaryresource .FileBinaryResource ;
16
+ import com .facebook .cache .common .CacheKey ;
17
+ import com .facebook .common .internal .Closeables ;
18
+ import com .facebook .common .memory .PooledByteBuffer ;
19
+ import com .facebook .common .memory .PooledByteBufferInputStream ;
20
+ import com .facebook .common .references .CloseableReference ;
21
+ import com .facebook .datasource .DataSource ;
22
+ import com .facebook .datasource .DataSources ;
23
+ import com .facebook .imagepipeline .cache .DefaultCacheKeyFactory ;
24
+ import com .facebook .imagepipeline .core .ImagePipeline ;
5
25
import com .facebook .imagepipeline .core .ImagePipelineFactory ;
26
+ import com .facebook .imagepipeline .request .ImageRequest ;
27
+
28
+ import java .io .File ;
29
+ import java .io .FileInputStream ;
30
+ import java .io .IOException ;
31
+ import java .io .InputStream ;
32
+
33
+ import xyz .zpayh .hdimage .core .HDImageViewConfig ;
34
+ import xyz .zpayh .hdimage .core .HDImageViewFactory ;
35
+ import xyz .zpayh .hdimage .datasource .Interceptor ;
36
+ import xyz .zpayh .hdimage .datasource .OrientationInterceptor ;
37
+ import xyz .zpayh .hdimage .datasource .interceptor .Interceptors ;
38
+ import xyz .zpayh .hdimage .state .Orientation ;
39
+ import xyz .zpayh .hdimage .util .Preconditions ;
40
+ import xyz .zpayh .hdimage .util .UriUtil ;
41
+
42
+ import static xyz .zpayh .hdimage .state .Orientation .ORIENTATION_0 ;
43
+ import static xyz .zpayh .hdimage .state .Orientation .ORIENTATION_180 ;
44
+ import static xyz .zpayh .hdimage .state .Orientation .ORIENTATION_270 ;
45
+ import static xyz .zpayh .hdimage .state .Orientation .ORIENTATION_90 ;
6
46
7
47
/**
8
48
* 文 件 名: MainApplication
@@ -22,14 +62,114 @@ public void onCreate() {
22
62
ImagePipelineFactory .initialize (this );
23
63
24
64
// 与Fresco加载库结合,共享缓存
25
- //HDImageViewConfig config = HDImageViewConfig.newBuilder(this)
26
- //.addInterceptor(new FrescoInterceptor())
27
- // .addInterceptor(new GlideInterceptor(this))
28
- // .build();
29
- //HDImageViewFactory.initialize(config);
65
+ HDImageViewConfig config = HDImageViewConfig .newBuilder (this )
66
+ .addInterceptor (new FrescoInterceptor ())
67
+ //.addInterceptor(new GlideInterceptor(this))
68
+ //.addOrientationInterceptor(new GlideOrientationInterceptor(this))
69
+ .build ();
70
+ HDImageViewFactory .initialize (config );
71
+ }
72
+
73
+ public class FrescoInterceptor implements Interceptor {
74
+ @ Override
75
+ public BitmapRegionDecoder intercept (Chain chain ) throws IOException {
76
+ final Uri uri = chain .uri ();
77
+ BitmapRegionDecoder decoder = chain .chain (uri );
78
+ if (decoder != null ){
79
+ return decoder ;
80
+ }
81
+
82
+ if (com .facebook .common .util .UriUtil .isNetworkUri (uri )){
83
+ ImagePipeline imagePipeline = ImagePipelineFactory .getInstance ().getImagePipeline ();
84
+
85
+ ImageRequest request = ImageRequest .fromUri (uri );
86
+ DataSource <CloseableReference <PooledByteBuffer >> dataSource = imagePipeline .fetchEncodedImage (request ,null );
87
+ try {
88
+ CloseableReference <PooledByteBuffer > ref = DataSources .waitForFinalResult (dataSource );
89
+ if (ref == null ){
90
+ return null ;
91
+ }
92
+ PooledByteBuffer result = ref .get ();
93
+ if (BuildConfig .DEBUG ) {
94
+ Log .d ("FrescoInterceptor" , "从我这加载" );
95
+ }
96
+ try {
97
+ InputStream inputStream = new PooledByteBufferInputStream (result );
98
+ Closeables .closeQuietly (inputStream );
99
+ return BitmapRegionDecoder .newInstance (inputStream ,false );
100
+ } catch (IOException e ) {
101
+ e .printStackTrace ();
102
+ ImageRequest imageRequest =ImageRequest .fromUri (uri );
103
+ CacheKey cacheKey = DefaultCacheKeyFactory .getInstance ().getEncodedCacheKey (imageRequest ,null );
104
+ BinaryResource resource = ImagePipelineFactory .getInstance ().getMainFileCache ().getResource (cacheKey );
105
+ File file =((FileBinaryResource )resource ).getFile ();
106
+ if (BuildConfig .DEBUG ) {
107
+ Log .d ("FrescoInterceptor" , file .getName ());
108
+ }
109
+ return Interceptors .fixJPEGDecoder (file ,e );
110
+ }
111
+ } catch (Throwable throwable ) {
112
+ if (BuildConfig .DEBUG ) {
113
+ Log .d ("FrescoInterceptor" , "intercept: 加载失败了" );
114
+ }
115
+ throwable .printStackTrace ();
116
+ return null ;
117
+ }
118
+ }
119
+
120
+ return null ;
121
+ }
122
+ }
123
+
124
+ public class GlideOrientationInterceptor implements OrientationInterceptor {
125
+
126
+ private final RequestManager mRequestManager ;
127
+
128
+ public GlideOrientationInterceptor (Context context ) {
129
+ Preconditions .checkNotNull (context );
130
+
131
+ mRequestManager = Glide .with (context );
132
+ }
133
+
134
+ @ Override
135
+ public int getExifOrientation (@ NonNull Context context , String sourceUri ) {
136
+ int orientation = Orientation .ORIENTATION_EXIF ;
137
+ Log .d ("Sherlock" , "从这儿加载: " );
138
+ if (UriUtil .isNetworkUri (Uri .parse (sourceUri ))) {
139
+ FutureTarget <File > target = mRequestManager .downloadOnly ().load (Uri .parse (sourceUri )).submit ();
140
+ try {
141
+ File file = target .get ();
142
+ try {
143
+ ExifInterface exifInterface = new ExifInterface (new FileInputStream (file ));
144
+ int orientationAttr = exifInterface .getAttributeInt (ExifInterface .TAG_ORIENTATION ,
145
+ ExifInterface .ORIENTATION_NORMAL );
146
+ if (orientationAttr == ExifInterface .ORIENTATION_NORMAL ||
147
+ orientationAttr == ExifInterface .ORIENTATION_UNDEFINED ) {
148
+ orientation = ORIENTATION_0 ;
149
+ } else if (orientationAttr == ExifInterface .ORIENTATION_ROTATE_90 ) {
150
+ orientation = ORIENTATION_90 ;
151
+ } else if (orientationAttr == ExifInterface .ORIENTATION_ROTATE_180 ) {
152
+ orientation = ORIENTATION_180 ;
153
+ } else if (orientationAttr == ExifInterface .ORIENTATION_ROTATE_270 ) {
154
+ orientation = ORIENTATION_270 ;
155
+ } else {
156
+ Log .w ("Sherlock" , "Unsupported EXIF orientation: " + orientationAttr );
157
+ }
158
+ } catch (IOException e ) {
159
+ e .printStackTrace ();
160
+ Log .w ("Sherlock" , "Could not get EXIF orientation of image" );
161
+ }
162
+ } catch (Exception e ) {
163
+ e .printStackTrace ();
164
+ }
165
+ mRequestManager .clear (target );
166
+ }
167
+ Log .d ("Sherlock" , "返回了:" +orientation );
168
+ return orientation ;
169
+ }
30
170
}
31
171
32
- /* public class GlideInterceptor implements Interceptor {
172
+ public class GlideInterceptor implements Interceptor {
33
173
34
174
private final RequestManager mRequestManager ;
35
175
@@ -51,36 +191,20 @@ public BitmapRegionDecoder intercept(Chain chain) throws IOException {
51
191
FutureTarget <File > target = mRequestManager .downloadOnly ().load (uri ).submit ();
52
192
try {
53
193
File file = target .get ();
54
- Log.d("GlideInterceptor", "用GlideInterceptor加载回来"+file.getAbsolutePath());
55
- FileInputStream inputStream = new FileInputStream(file);
56
- test(inputStream);
57
- decoder = BitmapRegionDecoder.newInstance(new FileInputStream(file),false);
58
- } catch (InterruptedException e) {
59
- e.printStackTrace();
60
- } catch (ExecutionException e) {
194
+ try {
195
+ Log .d ("GlideInterceptor" , "用GlideInterceptor加载回来" +file .getAbsolutePath ());
196
+ decoder = BitmapRegionDecoder .newInstance (new FileInputStream (file ),false );
197
+ } catch (IOException e ) {
198
+ e .printStackTrace ();
199
+ return Interceptors .fixJPEGDecoder (file , e );
200
+ }
201
+
202
+ } catch (Exception e ) {
61
203
e .printStackTrace ();
62
204
}
63
205
mRequestManager .clear (target );
64
206
}
65
207
return decoder ;
66
208
}
67
209
}
68
-
69
- private void test(FileInputStream inputStream) throws IOException{
70
- ExifInterface exifInterface = new ExifInterface(inputStream);
71
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_COLOR_SPACE));
72
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_APERTURE_VALUE));
73
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_ARTIST));
74
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_BITS_PER_SAMPLE));
75
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_BRIGHTNESS_VALUE));
76
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_CFA_PATTERN));
77
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_COMPONENTS_CONFIGURATION));
78
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_COMPRESSED_BITS_PER_PIXEL));
79
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_COMPRESSION));
80
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_CONTRAST));
81
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_COPYRIGHT));
82
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_CUSTOM_RENDERED));
83
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_CUSTOM_RENDERED));
84
- Log.d("Application",""+exifInterface.getAttribute(ExifInterface.TAG_DATETIME));
85
- }*/
86
210
}
0 commit comments