Java 如何从多个图像中生成PDF
我正在构建一个应用程序,将图像上传到我的公司服务器 我现在正在拍摄多幅图像,并将其转换为PDF格式,以便同一文档中的图像可以保持在一起 我的问题是我不知道如何制作,这样我就可以将多个图像添加到PDF创建中 我正在使用Kosal Geek的android库PhotoUtils进行图像处理 和PDFdocument来创建pdf 目前,单个图像可以工作,但如果我选择多个图像,就会出现空指针异常 主代码Java 如何从多个图像中生成PDF,java,android,android-studio,pdfdocument,Java,Android,Android Studio,Pdfdocument,我正在构建一个应用程序,将图像上传到我的公司服务器 我现在正在拍摄多幅图像,并将其转换为PDF格式,以便同一文档中的图像可以保持在一起 我的问题是我不知道如何制作,这样我就可以将多个图像添加到PDF创建中 我正在使用Kosal Geek的android库PhotoUtils进行图像处理 和PDFdocument来创建pdf 目前,单个图像可以工作,但如果我选择多个图像,就会出现空指针异常 主代码 public class PdfMake extends AppCompatActivity {
public class PdfMake extends AppCompatActivity {
private final int PICK_IMAGE=12345;
private final int REQUEST_CAMERA=6352;
CameraPhoto cameraPhoto;
GalleryPhoto galleryPhoto;
String selectedPhoto;
@Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.VmPolicy.Builder builder4=new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder4.build());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_make);
Button fromCamera=findViewById(R.id.SelectImages2);
Button PdfMake=findViewById(R.id.button4);
fromCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPictureDialog();
}
});
PdfMake.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api=Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View v) {
try {
createPdf();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void showPictureDialog () {
AlertDialog.Builder pictureDialog=new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems={
"Select photo from gallery",
"Capture photo from camera"};
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
getImageFromGallery();
break;
case 1:
try {
getImageFromCamera();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
});
}
@RequiresApi(api=Build.VERSION_CODES.KITKAT)
private void createPdf() throws IOException {
// create a new document
PdfDocument document=new PdfDocument();
// crate a page description
PdfDocument.PageInfo pageInfo=new PdfDocument.PageInfo.Builder(3000, 6000, 1).create();
// start a page
PdfDocument.Page page=document.startPage(pageInfo);
Canvas canvas=page.getCanvas();
Bitmap image=BitmapFactory.decodeFile(selectedPhoto);
canvas.drawBitmap(image, 10, 10, null);
document.finishPage(page);
String directory_path=Environment.getExternalStorageDirectory().getPath() + "/mypdf/";
File file=new File(directory_path);
if (!file.exists()) {
file.mkdirs();
}
String targetPdf=directory_path + "test-2.pdf";
File filePath=new File(targetPdf);
try {
document.writeTo(new FileOutputStream(filePath));
Toast.makeText(this, "Done", Toast.LENGTH_LONG).show();
} catch (IOException e) {
Log.e("main", "error " + e.toString());
Toast.makeText(this, "Something wrong: " + e.toString(), Toast.LENGTH_LONG).show();
}
// close the document
document.close();
}
private void getImageFromCamera () throws IOException {
cameraPhoto=new CameraPhoto(getApplicationContext());
Intent in=cameraPhoto.takePhotoIntent();
startActivityForResult(in, REQUEST_CAMERA);
Bungee.split(PdfMake.this);
}
private void getImageFromGallery () {
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
}
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
Bungee.split(PdfMake.this);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
String photoPath=cameraPhoto.getPhotoPath();
selectedPhoto=photoPath;
try {
Bitmap bitmap=ImageLoader.init().from(photoPath).requestSize(512, 512).getBitmap();
} catch (FileNotFoundException e) {
Toasty.error(getApplicationContext(),
"Something Wrong while loading photos", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == PICK_IMAGE) {
Uri uri=data.getData();
galleryPhoto.setPhotoUri(uri);
String photoPath=galleryPhoto.getPath();
selectedPhoto=photoPath;
try {
Bitmap bitmap=ImageLoader.init().from(photoPath).requestSize(512, 512).getBitmap();
} catch (FileNotFoundException e) {
Toasty.error(getApplicationContext(),
"Something Wrong while choosing photos", Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
int CAMERA_RESULT=11100;
if(requestCode == CAMERA_RESULT){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
try {
getImageFromCamera();
} catch (IOException e) {
e.printStackTrace();
}
}
else{
Toasty.error(getApplicationContext(), "Permission Needed.", Toast.LENGTH_LONG).show();
}
}
else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
if(requestCode == PICK_IMAGE){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED){
getImageFromGallery();
}
else{
Toasty.error(getApplicationContext(), "Permission Needed.", Toast.LENGTH_LONG).show();
}
}
else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
画廊图片代码
public class GalleryPhoto {
final String TAG = this.getClass().getSimpleName();
private Context context;
private Uri photoUri;
public void setPhotoUri(Uri photoUri) {
this.photoUri = photoUri;
}
public GalleryPhoto(Context context) {
this.context = context;
}
public Intent openGalleryIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction("android.intent.action.GET_CONTENT");
return Intent.createChooser(intent, this.getChooserTitle());
}
public String getChooserTitle() {
return "Select Pictures";
}
public String getPath() {
String path;
if (VERSION.SDK_INT < 11) {
path = RealPathUtil.getRealPathFromURI_BelowAPI11(this.context, this.photoUri);
} else if (VERSION.SDK_INT < 19) {
path = RealPathUtil.getRealPathFromURI_API11to18(this.context, this.photoUri);
} else {
path = RealPathUtil.getRealPathFromURI_API19(this.context, this.photoUri);
}
return path;
}
}
有一个很好的图书馆叫 您要做的是创建一个PDF对象和其中的一个页面
PDFWriter mPDFWriter = new PDFWriter(PaperSize.FOLIO_WIDTH, PaperSize.FOLIO_HEIGHT);
然后,您可以通过addImage
向其添加图像,并通过newPage()
您可以在项目中看到演示。相关:如何将此库与我的应用程序集成,我找不到库的.JAR文件。。我真的不知道;我不知道如何添加一个不在JAR文件中的库。有什么建议吗?我没有注意到这个库很旧。你可以下载它并在本地编译,但你可能会陷入混乱。另一种选择是将文件复制到项目中,并与项目一起编译。这意味着你将没有图书馆。最后,我在这里找到了一个JAR文件:谢谢,不管怎样,这个库很古老,就像你说的,我正陷入一个渐进的混乱和不推荐的依赖等等。我将尝试遵循PDFdocuments官方android开发教程,也许我可以从那里学到一些东西
PDFWriter mPDFWriter = new PDFWriter(PaperSize.FOLIO_WIDTH, PaperSize.FOLIO_HEIGHT);