More encoding related changes for encoding Chinese chars in QR codes
[zxing.git] / android / src / com / google / zxing / client / android / encode / EncodeThread.java
1 /*
2  * Copyright (C) 2010 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.google.zxing.client.android.encode;
18
19 import android.graphics.Bitmap;
20 import android.os.Handler;
21 import android.os.Message;
22 import android.util.Log;
23 import com.google.zxing.BarcodeFormat;
24 import com.google.zxing.WriterException;
25 import com.google.zxing.client.android.R;
26
27 final class EncodeThread extends Thread {
28
29   private static final String TAG = EncodeThread.class.getSimpleName();
30
31   private final String contents;
32   private final Handler handler;
33   private final int pixelResolution;
34   private final BarcodeFormat format;
35
36   EncodeThread(String contents, Handler handler, int pixelResolution, BarcodeFormat format) {
37     this.contents = contents;
38     this.handler = handler;
39     this.pixelResolution = pixelResolution;
40     this.format = format;
41   }
42
43   @Override
44   public void run() {
45     try {
46       Bitmap bitmap = QRCodeEncoder.encodeAsBitmap(contents, format, pixelResolution, pixelResolution);
47       Message message = Message.obtain(handler, R.id.encode_succeeded);
48       message.obj = bitmap;
49       message.sendToTarget();
50     } catch (WriterException e) {
51       Log.e(TAG, "Could not encode barcode", e);
52       Message message = Message.obtain(handler, R.id.encode_failed);
53       message.sendToTarget();
54     } catch (IllegalArgumentException e) {
55       Log.e(TAG, "Could not encode barcode", e);
56       Message message = Message.obtain(handler, R.id.encode_failed);
57       message.sendToTarget();
58     }
59   }
60 }