Android拼接合并图片生成长图
代码实现合并两张图片,以第一张图片的宽度为标准,如果被合并的第二张图片宽度和第一张不同,那么就以第一张图片的宽度为准线,对第二张图片进行缩放。
假设根目录的Pictures下已经有两张图片zhang.jpg和phil.jpg,最终把这两张图片合并成zhangphil.jpg的长图:
package zhangphil.test;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class JavaActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawable_activity);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
merge();
}
});
}
private void merge() {
new Thread(new Runnable() {
@Override
public void run() {
File zhang = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "zhang.jpg");
File phil = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "phil.jpg");
try {
Bitmap bitmap1 = BitmapFactory.decodeStream(new FileInputStream(zhang));
Bitmap bitmap2 = BitmapFactory.decodeStream(new FileInputStream(phil));
Bitmap newBmp = newBitmap(bitmap1, bitmap2);
File zhangphil = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "zhangphil.jpg");
if (!zhangphil.exists())
zhangphil.createNewFile();
save(newBmp, zhangphil, Bitmap.CompressFormat.JPEG, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
public static Bitmap newBitmap(Bitmap bmp1, Bitmap bmp2) {
Bitmap retBmp;
int width = bmp1.getWidth();
if (bmp2.getWidth() != width) {
//以第一张图片的宽度为标准,对第二张图片进行缩放。
int h2 = bmp2.getHeight() * width / bmp2.getWidth();
retBmp = Bitmap.createBitmap(width, bmp1.getHeight() + h2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(retBmp);
Bitmap newSizeBmp2 = resizeBitmap(bmp2, width, h2);
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(newSizeBmp2, 0, bmp1.getHeight(), null);
} else {
//两张图片宽度相等,则直接拼接。
retBmp = Bitmap.createBitmap(width, bmp1.getHeight() + bmp2.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(retBmp);
canvas.drawBitmap(bmp1, 0, 0, null);
canvas.drawBitmap(bmp2, 0, bmp1.getHeight(), null);
}
return retBmp;
}
public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
float scaleWidth = ((float) newWidth) / bitmap.getWidth();
float scaleHeight = ((float) newHeight) / bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bmpScale = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return bmpScale;
}
/**
* 保存图片到文件File。
*
* @param src 源图片
* @param file 要保存到的文件
* @param format 格式
* @param recycle 是否回收
* @return true 成功 false 失败
*/
public static boolean save(Bitmap src, File file, Bitmap.CompressFormat format, boolean recycle) {
if (isEmptyBitmap(src))
return false;
OutputStream os;
boolean ret = false;
try {
os = new BufferedOutputStream(new FileOutputStream(file));
ret = src.compress(format, 100, os);
if (recycle && !src.isRecycled())
src.recycle();
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
/**
* Bitmap对象是否为空。
*/
public static boolean isEmptyBitmap(Bitmap src) {
return src == null || src.getWidth() == 0 || src.getHeight() == 0;
}
}
合并后生成的长图:
添加权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />