0

我正在开发一个应用程序,您可以在单击获取相机访问权限并拍摄一些照片后立即访问并创建一个文件夹,并将其保存在最新的 pdf 中。但我不知道如何将 jpg 转换为 pdf。我读了一些关于 iText 但我不知道如何实现它

public class CameraView extends Activity implements SurfaceHolder.Callback{

    protected static final String TAG = null;

    /* VARIABILI PRIVATE */
    private SurfaceView mSurfaceView;
    private SurfaceHolder mSurfaceHolder;
    private Camera mCamera;
    private boolean mPreviewRunning;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFormat(PixelFormat.TRANSPARENT); //aggiungo il traslucido
        requestWindowFeature(Window.FEATURE_NO_TITLE);  //no barra titolo
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);   //full screen

        setContentView(R.layout.main);

        ImageButton buttonPicture = (ImageButton) findViewById(R.id.camera_surface_button);
        buttonPicture.setOnClickListener(new OnClickListener(){
            public void onClick(View v) {

                mCamera.takePicture(null, null, jpegCallback);
            }
        });

        mSurfaceView = (SurfaceView)findViewById(R.id.camera_surface);
        mSurfaceHolder = mSurfaceView.getHolder();  //recupero l'holder della surfaceview
        mSurfaceHolder.addCallback(this);   //faccio la bind alla nostra activity
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);    //tipo di surface, suggerito nei tutorial ufficiali
    }

    PictureCallback jpegCallback = new PictureCallback() {

            public void onPictureTaken(byte[] data, Camera camera) {
                //riparte la preview della camera

                File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                if (pictureFile == null){
                    Throwable e = null;
                    Log.d(TAG, "Error creating media file, check storage permissions: " +
                        e.getMessage());
                    return;
                }

                try {
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    fos.write(data);
                    fos.close();
                } catch (FileNotFoundException e) {
                    Log.d(TAG, "File not found: " + e.getMessage());
                } catch (IOException e) {
                    Log.d(TAG, "Error accessing file: " + e.getMessage());
                }

                mCamera.startPreview();
            }

        };

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        if (mPreviewRunning)
            mCamera.stopPreview();

        //setto le preferenze
        Camera.Parameters p = mCamera.getParameters();  //prendo le preferenze della camera
        p.setPreviewSize(arg2, arg3);
        ArrayList<Size> list = (ArrayList<Size>) p.getSupportedPictureSizes();  //recuepro le risoluzioni supportate dalla camera
        int picture_width = list.get(list.size()-1).width;
        int picture_height = list.get(list.size()-1).height;
        p.setPictureSize(picture_width, picture_height);    //setto la camera alla risoluzione pi bassa
        p.setJpegQuality(80);   // qualitˆ compressione JPEG

        // salvo le pref
        mCamera.setParameters(p);
        try {
            //lancio la preview
            mCamera.setPreviewDisplay(arg0);
            mCamera.startPreview();
            mPreviewRunning = true;
        } catch (IOException e) {
            //gestione errore
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = Camera.open();

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mCamera.stopPreview();
        mPreviewRunning = false;
        mCamera.release();

    }


    public static final int MEDIA_TYPE_IMAGE = 1;

    final Uri getOutputMediaFileUri(int type){
          return Uri.fromFile(getOutputMediaFile(type));
    }

    /** Create a File for saving an image or video */
    final  File getOutputMediaFile(int type){

        String direct = this.getIntent().getStringExtra("key");

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                  Environment.DIRECTORY_PICTURES ),direct );
        // This location works best if you want the created images to be shared
        // between applications and persist after your app has been uninstalled.

        // Create the storage directory if it does not exist
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                Log.d("MyCameraApp", "failed to create directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;

        if (type == MEDIA_TYPE_IMAGE){
             mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                        "IMG_"+ timeStamp + ".jpg");
        }  else {
            return null;
        }

        return mediaFile;
    }

}

帮我一把谢谢

4

1 回答 1

0

如果您有一个 Web 服务器进行转换, mpdf tcpdf fpdf

于 2013-08-23T22:19:59.757 回答