ClipImageActivity.java
5.2 KB
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
package com.hloong.clipheadicon;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 裁剪头像图片的Activity
*
*/
public class ClipImageActivity extends Activity{
private ClipImageLayout mClipImageLayout = null;
private ImageView ivBack;
private TextView tvUse;
public static int widthScale = 1;
public static int heightScale = 1;
public static final String IMAGE_FILE_NAME = "clip_temp.jpg";
public static final String RESULT_PATH = "result_path";
public static final String PASS_PATH = "pass_path";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
widthScale = getIntent().getIntExtra("widthScale",1);
heightScale = getIntent().getIntExtra("heightScale",1);
setContentView(R.layout.activity_clip_image);
ivBack = (ImageView) findViewById(R.id.iv_back);
tvUse = (TextView)findViewById(R.id.tv_use);
tvUse.setOnClickListener(l);
ivBack.setOnClickListener(l);
mClipImageLayout = (ClipImageLayout) findViewById(R.id.clipImageLayout);
String path = getIntent().getStringExtra(PASS_PATH);
// 有的系统返回的图片是旋转了,有的没有旋转,所以处理
int degreee = readBitmapDegree(path);
Bitmap bitmap = createBitmap(path);
if (bitmap != null) {
if (degreee == 0) {
mClipImageLayout.setImageBitmap(bitmap);
} else {
mClipImageLayout.setImageBitmap(rotateBitmap(degreee, bitmap));
}
} else {
finish();
}
}
View.OnClickListener l = new View.OnClickListener() {
@Override
public void onClick(View v) {
if(v.getId()==R.id.tv_use)
{
Bitmap bitmap = mClipImageLayout.clip();
String path = Environment.getExternalStorageDirectory() + File.separator+ IMAGE_FILE_NAME;
saveBitmap(bitmap, path);
Intent intent = new Intent();
intent.putExtra(RESULT_PATH, path);
setResult(RESULT_OK, intent);
finish();
}
else if(v.getId()==R.id.iv_back)
{
finish();
}
}
};
private void saveBitmap(Bitmap bitmap, String path) {
File f = new File(path);
if (f.exists()) {
f.delete();
}
FileOutputStream fOut = null;
try {
f.createNewFile();
fOut = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (fOut != null)
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 创建图片
*
* @param path
* @return
*/
private Bitmap createBitmap(String path) {
if (path == null) {
return null;
}
BitmapFactory.Options opts = new BitmapFactory.Options();
//不在内存中读取图片的宽高
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opts);
int width = opts.outWidth;
opts.inSampleSize = width > 1080 ? (int)(width / 1080) : 1 ;//注意此处为了解决1080p手机拍摄图片过大所以做了一定压缩,否则bitmap会不显示
opts.inJustDecodeBounds = false;// 这里一定要将其设置回false,因为之前我们将其设置成了true
opts.inPurgeable = true;
opts.inInputShareable = true;
opts.inDither = false;
opts.inPurgeable = true;
FileInputStream is = null;
Bitmap bitmap = null;
try {
is = new FileInputStream(path);
bitmap = BitmapFactory.decodeFileDescriptor(is.getFD(), null, opts);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
is = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
// 读取图像的旋转度
private int readBitmapDegree(String path) {
int degree = 0;
if(TextUtils.isEmpty(path)){
return degree;
}
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
// 旋转图片
private Bitmap rotateBitmap(int angle, Bitmap bitmap) {
// 旋转图片 动作
Matrix matrix = new Matrix();
matrix.postRotate(angle);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
bitmap.getWidth(), bitmap.getHeight(), matrix, false);
return resizedBitmap;
}
}