2017-03-03 100 views
0

但是,我已經從圖庫中選擇了圖像,然後應該怎麼做,我無法理解。應該使用哪個庫?以及如何實施它?如何從Android中的圖像列表中創建視頻

在我下面的代碼中,如何從圖庫選擇中獲得ArrayList<File>images。我用過Jcodec Library。如果有其他解決方案,那麼請建議。

MainActivity.java:

public class MainActivity extends Activity { 

GridView gridGallery; 
Handler handler; 
GalleryAdapter adapter; 
Button btnGalleryPick; 
Button btnGalleryPickMul; 
ViewSwitcher viewSwitcher; 
ImageLoader imageLoader; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    initImageLoader(); 
    init(); 
} 

private void initImageLoader() { 
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() 
      .cacheOnDisc().imageScaleType(ImageScaleType.EXACTLY_STRETCHED) 
      .bitmapConfig(Bitmap.Config.RGB_565).build(); 
    ImageLoaderConfiguration.Builder builder = new ImageLoaderConfiguration.Builder(
      this).defaultDisplayImageOptions(defaultOptions).memoryCache(
      new WeakMemoryCache()); 
    ImageLoaderConfiguration config = builder.build(); 
    imageLoader = ImageLoader.getInstance(); 
    imageLoader.init(config); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == 200 && resultCode == Activity.RESULT_OK) { 

     String[] all_path = data.getStringArrayExtra("all_path"); 

     ArrayList<CustomGallery> dataT = new ArrayList<CustomGallery>(); 

     for (String string : all_path) { 
      CustomGallery item = new CustomGallery(); 
      item.sdcardPath = string; 
      dataT.add(item); 
     } 

     viewSwitcher.setDisplayedChild(0); 
     adapter.addAll(dataT); 
    } 
} 

private void init() { 

    handler = new Handler(); 
    gridGallery = (GridView) findViewById(R.id.gridGallery); 
    gridGallery.setFastScrollEnabled(true); 
    adapter = new GalleryAdapter(getApplicationContext(), imageLoader); 
    adapter.setMultiplePick(false); 
    gridGallery.setAdapter(adapter); 

    viewSwitcher = (ViewSwitcher) findViewById(R.id.viewSwitcher); 
    viewSwitcher.setDisplayedChild(1); 

    btnGalleryPick = (Button) findViewById(R.id.btnGalleryPick); 
    btnGalleryPick.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (gridGallery.getChildCount() == 0) { 
       Toast.makeText(getApplicationContext(), "Please Select Photo First", Toast.LENGTH_SHORT).show(); 
      } else { 
       String location = String.valueOf(Environment.getExternalStorageDirectory()); 
       File root = new File(Environment.getExternalStorageDirectory() 
         .getAbsolutePath() + "/Birthday/"); 
       ArrayList<File> fileList = new ArrayList<File>(); 
       File listFile[] = root.listFiles(); 
       if (listFile != null && listFile.length > 0) { 
        for (int i = 0; i < listFile.length; i++) { 
         /*if (listFile[i].isDirectory()) { 
          fileList.add(listFile[i]); 
          getfile(listFile[i]); 
         } else {*/ 
         if (listFile[i].getName().endsWith(".png") 
           || listFile[i].getName().endsWith(".jpg") 
           || listFile[i].getName().endsWith(".jpeg") 
           || listFile[i].getName().endsWith(".gif")) 
         { 
          fileList.add(listFile[i]); 
         } 
        } 
       } 
       softwareMakeVideo(fileList, location, "test"); 
      } 
     } 
    }); 
    btnGalleryPickMul = (Button) findViewById(R.id.btnGalleryPickMul); 
    btnGalleryPickMul.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Intent i = new Intent(Action.ACTION_MULTIPLE_PICK); 
      startActivityForResult(i, 200); 
     } 
    }); 
} 
public String softwareMakeVideo(ArrayList<File> images, String location, 
           String name) { 
    File directory = new File(location); 
    if (!directory.exists()) { 
     directory.mkdir(); 
    } 
    File file = new File(directory, name + ".mp4"); 
    try { 
     SequenceEncoder encoder = new SequenceEncoder(file); 
     for (Iterator<File> iterator = images.iterator(); iterator.hasNext();) { 
      File image = iterator.next(); 
      if (!image.exists() || image.length() == 0) { 
       continue; 
      } 
      Bitmap frame = BitmapFactory.decodeFile(image.getAbsolutePath()); 
      try { 
       encoder.encodeNativeFrame(this.fromBitmap(frame)); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
     encoder.finish(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return file.getAbsolutePath(); 
} 
public Picture fromBitmap(Bitmap src) { 
    Picture dst = Picture.create(src.getWidth(), src.getHeight(), ColorSpace.RGB); 
    fromBitmap(src, dst); 
    return dst; 
} 
private void fromBitmap(Bitmap src, Picture dst) { 
    int[] dstData = dst.getPlaneData(0); 
    int[] packed = new int[src.getWidth() * src.getHeight()]; 
    src.getPixels(packed, 0, src.getWidth(), 0, 0, src.getWidth(), src.getHeight()); 
    for (int i = 0, srcOff = 0, dstOff = 0; i < src.getHeight(); i++) { 
     for (int j = 0; j < src.getWidth(); j++, srcOff++, dstOff += 3) { 
      int rgb = packed[srcOff]; 
      dstData[dstOff] = (rgb >> 16) & 0xff; 
      dstData[dstOff + 1] = (rgb >> 8) & 0xff; 
      dstData[dstOff + 2] = rgb & 0xff; 
     } 
    } 
} 
} 
+0

你可以使用ffmpeg庫爲android – Piyush

+0

如何在android中實現ffmpeg –

回答