程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#生成條形碼圖片的簡略辦法

C#生成條形碼圖片的簡略辦法

編輯:C#入門知識

C#生成條形碼圖片的簡略辦法。本站提示廣大學習愛好者:(C#生成條形碼圖片的簡略辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#生成條形碼圖片的簡略辦法正文


本文實例講述了C#生成條形碼圖片的簡略辦法。分享給年夜家供年夜家參考。詳細完成辦法以下:

完成道理:

其實Windows自己就有一個字體是用來顯示條形碼的。
只需將數字改成這類字體就釀成了條形碼。

windows字體庫下,有以下八種字體可以用來將數字轉換成條形碼:
Code39AzaleaNarrow1
Code39AzaleaNarrow2
Code39AzaleaNarrow3
Code39AzaleaRegular1
Code39AzaleaRegular2
Code39AzaleaWide1
Code39AzaleaWide2
Code39AzaleaWide3

把代碼貼給年夜家參考:
Bitmap b=new Bitmap(200,200);
Graphics g = Graphics.FromImage(b);
Font font = new Font("Code39AzaleaRegular2", 32);
g.DrawString("123456", font, Brushes.Black, new PointF(100,100)); 
pictureBox1.BackgroundImage = b;
pictureBox1.BackgroundImageLayout = ImageLayout.Zoom

願望本文所述對年夜家的C#法式設計有所贊助。

jpg?201632815658" alt="" />

圖片加載出來了,我們要看看日記,日記不會撒謊,我截圖的日記

適才是當地和內存都沒有那就的去收集下載了,一切日記打印的是開啟收集下載
但是第二次進入運用的時刻呢?還歸去開啟收集下載嗎?那確定不會的,要不如許要它有啥用呢?
看看吧這就是第二次進入運用的時刻的日記

從文件中加載圖片,不在須要開啟收集下載直接從文件中獲得,好了,如許成績就來了,從文件中讀取確定效力欠好呀,沒有直接從內存中讀取快吧!我們挪用體系保護好的一個算法,比來起碼應用算法,這個算法是在我學操作體系的時刻學到的,其時先生講了一下,不外還好,能在項目頂用到先生講的器械,對此,認為就這個就完了?我們應當把文件中的圖片讀取到內存中,如許避免像listview如許的疾速滑動發生的錯位bug。上面看看我在疾速滑動時的後果:

加載的圖片都是從內存中獲得,如許在後果下面就很好的啦!
好的,這麼好的圖片三級緩存,那究竟怎樣完成的呢和究竟怎樣用的呢?上面就貼出我寫的代碼吧!讓小同伴看看,趁便優化優化!

package com.example.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v4.util.LruCache;
import android.util.Log;


/**
 * 圖片的三級緩存對象類{往後項目須要}
 * @author double 江
 *
 */
public class ImageCachceUitl {
  public static final int SUCCSEE = 0;
  public static final int FAIL = 1;
  private Context context;
  private LruCache<String, Bitmap> cache;//lru算法聚集,string是圖片的url,bitmap為圖片的值類型
  private File cacheDir;
  Handler handler;
  private ExecutorService executorService;//保護線性池
  public ImageCachceUitl(Context context ,Handler handler){
    this.context=context;
    this.handler=handler;
    cacheDir=context.getCacheDir();//取得cache文件夾
    //保護幾個收集線程下載圖片
    executorService=Executors.newFixedThreadPool(5);
    int maxSize=(int) (Runtime.getRuntime().maxMemory()/8);//取得運轉情況的內存年夜小的1/8
    cache=new LruCache<String, Bitmap>(maxSize){
      // TODO 每存儲一張圖片的年夜小(感化於內存溢出丟圖片)
      @Override
      protected int sizeOf(String key, Bitmap value) {
        //前往以後一行所占的字節數*高度,就是圖片的年夜小
        return value.getRowBytes()*value.getHeight();
      }
    };//以後圖片緩存總數的年夜小
  }
  /**
   * 
   * @param url 下載圖片的銜接
   * @param position 須要顯示圖片的imgeView的Tag
   * @return
   */
  public Bitmap getBitmapFromUrl(String url,int position){
    //1內存中獲得圖片LRU算法
    Bitmap bitmap=cache.get(url);
    //內存中有指定圖片
    if (bitmap!=null) {
      Log.i("從內存中取得圖片", "從內存中取得圖片"+url);
      return bitmap;
    }
    //2文件中獲得圖片
    bitmap=getBitmapFromFile(url);
    if (bitmap!=null) {
      Log.i("從文件中取得圖片", "從文件中取得圖片"+url);
      return bitmap;
    }
    //3開啟收集下載
    Log.i("從收集中取得圖片", "從收集中取得圖片"+url);
    getBitmapFromNet(url,position);
    return null;

  }

  /**
   * 收集獲得圖片
   * @param url 圖片的;鏈接地址
   * @param position 須要顯示的imageview的tag
   */
  private void getBitmapFromNet(String url, int position) {

    //開啟義務
    executorService.execute(new RunnableTask(url,position));
  }
  //義務接口
  class RunnableTask implements Runnable{
    String imageUrl;
    int position;
    private HttpURLConnection httpURLConnection;
    public RunnableTask(String imageUrl, int position) {
      this.position=position;
      this.imageUrl=imageUrl;
    }

    @Override
    public void run() {
      //子線程的操作,開啟收集下載圖片
      try {
        URL url=new URL(imageUrl);
        //要求對象
         httpURLConnection=(HttpURLConnection) url.openConnection();
        //收集要求的方法
        httpURLConnection.setRequestMethod("GET");
        //超時的時光,
        httpURLConnection.setConnectTimeout(5000);
        //讀取超時的時光
        httpURLConnection.setReadTimeout(5000);
        //httpURLConnection.getResponseCode()拿到最新數據
        if (httpURLConnection.getResponseCode()==200) {
          //success get data from net;get tape 
          InputStream inputStream=httpURLConnection.getInputStream();
          //將流轉化成bitmap圖片
          Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
          //應用handler機制放入主線程中顯示
          Message msg=new Message();
          //須要在主線程中顯示的圖片msg.obj
          msg.obj=bitmap;
          msg.arg1=position;
          //為msg設置標志
          msg.what=SUCCSEE;
          handler.sendMessage(msg);
          //一,將下載完後的圖片保留到內存中
          cache.put(imageUrl, bitmap);
          //二,將下載完後的圖片保留到文件中
          writeToLoce(imageUrl,bitmap);
          return;
        }


      } catch (Exception e) {
        e.printStackTrace();
      }
      //封閉要求
      finally{
        //斷開辦事器
        if (httpURLConnection!=null) {
          httpURLConnection.disconnect();
        }        
      }
      //發送一個空新聞
      handler.obtainMessage(FAIL).sendToTarget();
    }
  }
  /**
   * 圖片寫入cache文件夾上面的操作
   * @param imageUrl
   * @param bitmap
   */
  private void writeToLoce(String imageUrl, Bitmap bitmap) {
    try {
      String bitmapefilename=MD5Encoder.encode(imageUrl).substring(10);
      Log.i("bitmapefilename", bitmapefilename);
      File file=new File(cacheDir, bitmapefilename);
      FileOutputStream fileOutputStream =new FileOutputStream(file);
      //寫入文件的操作(1圖片類型2圖片質量當為100時表現不緊縮3文件流)
      bitmap.compress(CompressFormat.JPEG, 100, fileOutputStream);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  /**
   * 讀取文件中圖片的操作
   * @param url 圖片的銜接地址
   * @return
   */
  private Bitmap getBitmapFromFile(String url) {
    try {
      //應用Md5對象加密截取前10個字符串
      String bitmapefilename=MD5Encoder.encode(url).substring(10);
      /**
       * 在cache文件夾上面找到指定文件
       * dir cache文件存儲途徑
       * name 文件稱號
       */
      File file=new File(cacheDir, bitmapefilename);
    // file.mkdir();
      //FileInputStream fileInputStream=new FileInputStream(file);
      Bitmap bitmap=BitmapFactory.decodeFile(file.getPath());//完全文件途徑
      //Log.i("文件途徑", file.getPath().toString());
      //2讀取以後放入內存,進步效力
      cache.put(url, bitmap);
      return bitmap;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return null;

  }
}

上面就是一個應用demo了,其實很簡略


package com.example.do0me;
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.security.PublicKey; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.MarshalBase64; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import org.xmlpull.v1.XmlPullParserException; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.os.Handler; import android.util.Base64; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.view.ViewTreeObserver.OnPreDrawListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.ImageView; import android.widget.ListView; import android.widget.Toast; import com.example.util.AgbcApi; import com.example.util.ClippingPicture; import com.example.util.FastBlur; import com.example.util.ImageCachceUitl; import com.tencent.connect.avatar.c; /** * 圖片三級緩存的測試+圖片上傳 * @author double 江 * */ public class MainActivity4 extends Activity { private ImageCachceUitl imageCachceUitl; private List<String> urlList=new ArrayList<String>(); private Runnable runnable; private Handler handler =new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case ImageCachceUitl.SUCCSEE: Bitmap bitmap=(Bitmap) msg.obj; int psition=msg.arg1; //經由過程TAg加載以後的limageview ImageView imageView=(ImageView) listview.findViewWithTag(psition); if (null!=bitmap&&null!=imageView) { imageView.setImageBitmap(bitmap); } break; case ImageCachceUitl.FAIL: Toast.makeText(getApplicationContext(), "下載毛病", Toast.LENGTH_LONG).show(); default: break; } }; }; private ListView listview; private SoapObject request; private ExecutorService executorService; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main4); listview=(ListView) findViewById(R.id.imageviewlist); imageCachceUitl=new ImageCachceUitl(getApplicationContext(), handler); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg");urlList.add("http://ww4.sinaimg.cn/large/90bd89ffjw1eqvmd6o8r6j20go0p5ju2.jpg"); listview.setAdapter(new myListAdapt()); executorService=Executors.newFixedThreadPool(5); executorService.execute(new Runnable() { @Override public void run() { getImageromSdk(); } }); } public String getImageFromAndroid(String arg0,String arg1, String arg2){ Log.i("進入端口辦法", "進入端口辦法"); final String methodName="getImageFromAndroid"; final String soapAction=AgbcApi.NAMESPACE+methodName; request = new SoapObject(AgbcApi.NAMESPACE, methodName); request.addProperty("arg0",arg1); request.addProperty("arg1",arg2); SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); (new MarshalBase64()).register(envelope); envelope.bodyOut = request; envelope.dotNet=false; envelope.setOutputSoapObject(request); HttpTransportSE ht = new HttpTransportSE(AgbcApi.TASKSERVICEURL); ht.debug=true; try { ht.call(soapAction, envelope); Log.i("要求", envelope.bodyIn.toString()); } catch (IOException | XmlPullParserException e) { e.printStackTrace(); } return arg1; }; @SuppressLint("SdCardPath") public void getImageromSdk(){ Log.i("進入獲得圖片辦法", "進入獲得圖片辦法"); try{ String srcUrl = "/sdcard/"; //途徑 String fileName = "1.png"; //文件名 String filrName2="2.jpg"; List<String>imageList=new ArrayList<>(); imageList.add(fileName); imageList.add(filrName2); for (int i = 0; i < imageList.size(); i++) { FileInputStream fis = new FileInputStream(srcUrl + imageList.get(i)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int count = 0; while((count = fis.read(buffer)) >= 0){ baos.write(buffer, 0, count); } String uploadBuffer = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT)); //停止Base64編碼 String methodName = "uploadImage"; getImageFromAndroid(methodName,imageList.get(i), uploadBuffer); //挪用webservice Log.i("connectWebService", "start"); fis.close(); } }catch(Exception e){ e.printStackTrace(); } } class myListAdapt extends BaseAdapter{ private LayoutInflater layoutInflater; ImageView list_imag; Button list_but; @Override public int getCount() { // TODO Auto-generated method stub return urlList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return urlList.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } @SuppressLint({ "InflateParams", "ViewHolder" }) @Override public View getView(final int position, View convertView, ViewGroup parent) { layoutInflater=LayoutInflater.from(getApplication()); //if (layoutInflater==null) { convertView = layoutInflater.inflate(R.layout.image_list_item, null); // } list_but=(Button) convertView.findViewById(R.id.list_but); list_imag=(ImageView) convertView.findViewById(R.id.list_imag); list_imag.setTag(position); final Bitmap bitmap=imageCachceUitl.getBitmapFromUrl(urlList.get(position), position); if (null!=bitmap) { list_imag.setImageBitmap(bitmap); } list_imag.setVisibility(View.VISIBLE); list_but.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //urlList.get(position); Log.i("獲得點擊核心", "獲得點擊核心"); // getImageromSdk(); //startRun(); new Thread(new Runnable() { @Override public void run() { getImageromSdk(); } }).start(); } }); return convertView; } } }

就為年夜家引見到這吧,年夜家漸漸消化,願望文章對年夜家有或多或少的贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved